Hachemi Hamadi
06/15/2021, 3:20 PMclass ProjectsViewModel : ViewModel()
and i already called it in a component using :
val projectsViewModel: ProjectsViewModel = viewModel()
and when i navigate (using Navigation Component) and try to get the model using the same viewModel through different components ?
Or should i declare the instance in the top of my code and pass it to different component that need access to it ?Ian Lake
06/15/2021, 5:51 PMviewModel()
you create in one composable destination will be scoped to that destination, so calling viewModel()
in a different destination will give you a different instance (scoped to that other destination). There are a number of APIs for scoping ViewModels to a navigation graph or getting the previous destination's NavBackStackEntry (i.e., to return a result). What is your use case?