Hi, just would like to share and confirm. This is how I scope my
ViewModel
to a
NavGraph
.
Copy code
@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)