is it possible to define a viewmodel at the naviga...
# compose
m
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
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
Interesting, I'm gonna try to do some experiments to explore the possibilities
thanks