Skip to content

ViewModel scoped to a nav graph #720

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fvito opened this issue Feb 14, 2025 · 2 comments
Open

ViewModel scoped to a nav graph #720

fvito opened this issue Feb 14, 2025 · 2 comments
Labels
feedback needed Extra attention is needed

Comments

@fvito
Copy link

fvito commented Feb 14, 2025

Hello,

This is more of a question, but how does one manage to correctly share the ViewModel between destinations in a nav graph.. eg scoping the ViewModel to the nav graph

I looked at the documentation, but I failed to make it work, because the generated NavGraphs don't contain the subgraph.. eg in the documentation there is NavGraphs.settings but such field does not get generated

I tried passing the nav graph directly, but it crashes because it does not implement Serializer. The solution I ended up using was something like this

navGraph(MyNavGraph) {
            val parentEntry = remember(navBackStackEntry) {
                navController.getBackStackEntry(MyNavGraph.route)
            }
            dependency(hiltViewModel<MyViewModel>(parentEntry))
}

Is this the correct approach or am I missing something?

I also saw #19 but it also uses the generated field that does not get generated again and also it looks like it would need to be placed in the main app module due to the way dependencies are done.

@raamcosta
Copy link
Owner

What version of the library are you using?

@raamcosta raamcosta added the feedback needed Extra attention is needed label Mar 6, 2025
@fvito
Copy link
Author

fvito commented May 12, 2025

I'm using version 2.1.0-beta15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback needed Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants