#compose #viewmodels
Hi everyone
Anyone have an idea about working with navigation compose with view models that have been created and cleared automatically within the composable scope and organise the code without any boilerplate?
I 'm trying to migrate from normal navigation component to the new navigation compose and I'm facing these problems
i
Ian Lake
04/25/2022, 11:27 PM
That's already how Navigation Compose works - any calls to
viewModel()
or
hiltViewModel()
are scoped to that destination
💯 1
a
Ahmed Shehata
04/26/2022, 1:44 AM
I tried to use
hiltViewModel()
but I noticed that the
onCleared()
of my VM never get called even I navigated to a different destination.
i
Ian Lake
04/26/2022, 1:46 AM
Well, navigating to another destination by default only adds a new destination to the back stack. The
onCleared()
gets called when a destination gets popped off the back stack. This is exactly how Navigation with Fragments work as well 🙂
❤️ 2
a
Ahmed Shehata
04/26/2022, 2:31 AM
Thank you for helping
I think that what I missed 😅