Archie
02/27/2021, 9:55 AMViewModel
to a NavGraph
.
@Composable
inline fun <reified VM : ViewModel> navGraphViewModel(
route: String,
navController: NavController,
): VM {
// Now get the NavBackStackEntry associated with the parent
val backStackEntry = navController.getBackStackEntry(route)
val context = LocalContext.current
return ViewModelLazy(
viewModelClass = VM::class,
storeProducer = { backStackEntry.viewModelStore },
factoryProducer = { HiltViewModelFactory(context, backStackEntry) }
).value
}
Was wondering how others are doing theres? (I am using Hilt for my project, the ViewModelFactory could be passed in if you are not using Hilt)jaqxues
02/27/2021, 10:15 AMArchie
02/27/2021, 10:17 AMviewModel(factory = HiltViewModelFactory(LocalContext.current, it))
fixes the issue atleast temporarily?jaqxues
02/27/2021, 10:19 AM