Have a question about the navigation component. I...
# compose
d
Have a question about the navigation component. If I instantiate a class that is not a viewModel in the NavHost within one of the compose route elements, will this object be disposed of when navigating to another route? All the examples I see are specific to viewModel objects here.
a
Could you explain the context? Usually you could instantiate the class/ inject it where you need in either the Composable file or the view model that would be used in it.
i
The
@Composable
lambda of a destination is disposed when you go to a new destination, yes - that old screen is no longer part of your UI
Any
rememberSaveable
objects will be saved and restored when you return to that screen
(and any
ViewModel
associated with that screen is indeed kept in memory until that screen is popped off the back stack)
m
Regarding your last comment @Ian Lake, Is it no longer recommended to make a singleactivity app? Before the viewmodel we instantiated them to the fragment life cycle but now they all go with the activity. Wouldn't you have a lot of viewmodel left in memory in a big app? what do you recommend?
i
If you are creating a
ViewModel
in a
composable
screen of a
NavHost
, it is tied to that screen, not to the activity. Exactly the same case as how a fragment based world worked
m
Really? Thanks for the info, is a good news for me, I was completely confused
d
@Ian Lake thanks for the clarification