Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix sharedKoinViewModel fails with type-safe navigation routes#2293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
nazaburo wants to merge3 commits intoInsertKoinIO:main
base:main
Choose a base branch
Loading
fromnazaburo:main

Conversation

@nazaburo
Copy link

@nazaburonazaburo commentedOct 19, 2025
edited
Loading

Fix sharedKoinViewModel fails withtype-safe navigation routes#2292

@nazaburonazaburo changed the titleFix issue (#2292)Fix sharedKoinViewModel fails with type-safe navigation routesOct 20, 2025
@arnaudgiuliani
Copy link
Member

I don't see why it bring stronger verification here, as the compiler should already help here.
I believe more in a K 2.2 compat problem.

@arnaudgiulianiarnaudgiuliani added this to the4.2.0 milestoneOct 20, 2025
@nazaburo
Copy link
Author

@arnaudgiuliani
Thanks! That makes sense.
However, I think this behavior comes from the fact that navGraphRoute is declared as Any?.
In that case, the compiler won’t treat it as a String, so the String function overload isn’t resolved without explicit handling.
To me, this looks like expected Kotlin behavior rather than a K2.2 compatibility issue.

@arnaudgiuliani
Copy link
Member

Do you have a test case to reproduce, something we can add into examples project, the android compose app?

Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Pull Request Overview

This PR fixes a bug wheresharedKoinViewModel fails when using type-safe navigation routes. The fix adds conditional handling to support both string-based and type-safe (object-based) navigation routes.

  • Adds type checking to handle both String and non-String route types
  • EnsuresgetBackStackEntry is called appropriately for both route formats

Tip: Customize your code reviews with copilot-instructions.md.Create the file orlearn how to get started.

Comment on lines +87 to +91
if (navGraphRouteisString) {
navController.getBackStackEntry(navGraphRoute)
}else {
navController.getBackStackEntry(navGraphRoute)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Both branches of the if-else statement execute identical code. The conditional check serves no purpose and should be removed, leaving onlynavController.getBackStackEntry(navGraphRoute).

Suggested change
if (navGraphRouteisString) {
navController.getBackStackEntry(navGraphRoute)
}else {
navController.getBackStackEntry(navGraphRoute)
}
navController.getBackStackEntry(navGraphRoute)

Copilot uses AI. Check for mistakes.
@nazaburo
Copy link
Author

@arnaudgiuliani
I've added test cases tosample-android-compose demonstrating the fix.

Added:

  • Type-Safe navigation routes (NavigationGraphRoute,FirstScreenRoute,SecondScreenRoute)
  • Two navigation examples:
    • Type-Safe Navigation (navigationTypeSafeGraph) - Uses@Serializable route objects
    • String-based Navigation (navigationStringGraph) - Uses traditional string routes
  • Two screens sharing aSharedViewModel viasharedKoinViewModel
  • Simple ViewModel to verify the same instance is shared between screens

You can test it by running the app and tapping "(>)Type Safe Navigation Example" or "(>)String Navigation Example". Both examples demonstrate that screens share the same ViewModel
instance (shown by matching instance IDs and shared timestamp), confirming the fix works correctly with Type-Safe routes.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotCopilot left review comments

@arnaudgiulianiarnaudgiulianiAwaiting requested review from arnaudgiuliani

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.0

Development

Successfully merging this pull request may close these issues.

2 participants

@nazaburo@arnaudgiuliani

[8]ページ先頭

©2009-2025 Movatter.jp