I have a `class ProjectsViewModel : ViewModel()` ...
# compose
h
I have a
class ProjectsViewModel : ViewModel()
and i already called it in a component using :
Copy code
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 ?
i
A
viewModel()
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?