Marko Novakovic
06/23/2021, 3:44 PMhilt-navigation
documentation.
inline fun <reified VM : ViewModel> hiltViewModel()
and
internal fun <VM : ViewModel> hiltViewModel(
kClass: KClass<VM>,
backStackEntry: NavBackStackEntry
)
and
hiltNavGraphViewModel()
documentation says that it gets or creates ViewModel
scoped to the current navigation graph. as far as I know and what I see in code if current ViewModelStoreOwned
is NavBackStackEntry
, which composables in graph are, ViewModel
is scoped to NavBackStackEntry
.
hiltViewModel<HomeViewModel>(backStackEntry)
and
hiltViewModel<HomeViewModel>()
should be the same right? it scopes ViewModel
to current back stack entry?Ian Lake
06/23/2021, 3:49 PMhiltViewModel()
will scope the ViewModel to the current NavBackStackEntry
i.e., your current composable destination. If you use navController.getBackStackEntry("your_nav_graph_route")
and pass that NavBackStackEntry
to hiltViewModel()
, then the ViewModel will be scoped to that navigation graph - navigation graphs, just like composable destinations, are on the back stack and have their own NavBackStackEntry
Ian Lake
06/23/2021, 3:53 PMMarko Novakovic
06/23/2021, 3:56 PMhiltViewModel(backStackEntry)
being scoped to graph but I will take a look at links, maybe I will understand better thenIan Lake
06/23/2021, 4:26 PMIan Lake
06/23/2021, 4:29 PMMarko Novakovic
06/23/2021, 5:17 PM