navigator
navigator copied to clipboard
ViewModel for Navigation graph level
is it possible to scope ViewModel to Navigation level currently?
Is this question for navigator (deals with fragments) or navigator-compose (which deals with Composables)?
@KaustubhPatange Sorry i didn't mention that, it is meant for navigator-compose
You can! All you have to do is scope ViewModelStoreOwner
to the parent Route
. I'll update the docs to mention this, but for now take a look at following snippet.
val controller = rememberNavController<MenuItem>()
.... // your navigation setup
/* Note: controller.parentRoute could be null so make sure to handle that gracefully as well.
* Cases where it could be null? When your navigation is not a nested destination. */
val viewModel by viewModel(viewModelStoreOwner = controller.parentRoute)
@KaustubhPatange
You mean like this ? val viewModel = viewModel<DiscoverViewModel>(viewModelStoreOwner = controller.parentRoute?.lifecycleController!!)
Yes, exactly!