https://kotlinlang.org logo
#compose
Title
# compose
m

mgrazianodecastro

03/10/2023, 4:49 PM
is it possible to define a viewmodel at the navigation root (of a nested graph) instead of the composable route (with hiltviewmodel, for example)? Or in that case is just better to pass it down from the activity? The goal would be to encapsulate the viewmodel into the specific navigation graph
i

Ian Lake

03/10/2023, 6:26 PM
Where the ViewModel is stored depends entirely on what ViewModelStoreOwner you pass into
viewModel
or
hiltViewModel
, so if you want it stored at the navigation graph level, pass it the
NavBackStackEntry
associated with that graph that you've retrieved via
navController.getBackStackEntry(yourGraphsRoute)
Even the root graph has a route - that's what the
route
parameter on the NavHost lets you set
m

mgrazianodecastro

03/10/2023, 6:36 PM
Interesting, I'm gonna try to do some experiments to explore the possibilities
thanks
7 Views