am a bit confused with `hilt-navigation` documenta...
# compose
m
am a bit confused with
hilt-navigation
documentation.
Copy code
inline fun <reified VM : ViewModel> hiltViewModel()
and
Copy code
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
.
Copy code
hiltViewModel<HomeViewModel>(backStackEntry)
and
Copy code
hiltViewModel<HomeViewModel>()
should be the same right? it scopes
ViewModel
to current back stack entry?
i
Correct, by default,
hiltViewModel()
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
m
I am still a bit confused with
hiltViewModel(backStackEntry)
being scoped to graph but I will take a look at links, maybe I will understand better then
i
Yeah, graph is the wrong term here. It is anything on the navigation back stack
👍 1
You can file a bug against Hilt to update their docs: https://issuetracker.google.com/issues/new?component=874079
👍 2
m
thank you very much