georgiy.shur
12/25/2020, 9:52 PMviewModel
extension, which should use the Hilt factory underneath and create my ViewModel
from it.
Everything works fine, but when I'm adding the Compose NavHost
to the mix, the composable inside it that calls viewModel
just uses the generic factory instead the one generated by Hilt. I think it has something to do with this code from the source:
public fun NavHost(navController: NavHostController, graph: NavGraph) {
var context = AmbientContext.current
val lifecycleOwner = AmbientLifecycleOwner.current
val viewModelStore = AmbientViewModelStoreOwner.current.viewModelStore
...
}
What is the right way to make them work together? I'm still not sure that I'm getting the Ambient concept right.ModelStoreOwner
normally is MyActivity
, which is annotated with AndroidEntryPoint
, so it knows how to inject the right view-model. But inside the NavHost
composable, the current ModelStoreOwner
is NavBackStackEntry
, created by the framework, so it knows nothing about Hilt injection. The question stands though, how to make them work together? 🙂Ian Lake
12/29/2020, 11:23 PM