ViewModels are still useful for passing data between screens or changing a Composable from another like in xml.
Let's say you have a Cart composable you wish to change TopAppBar from or other Composables that are not related to each other but changes TopAppbar color or text. You can use a shared ViewModel to set from various places and listen for mutableState inside TopAppBar composable.
Also, remember is for storing Data through recomposition, rememberSaveable adds configuration changes on top of remember. When you move to next screen and come back if your Composable is not in composition these will be reinstantiated but you can keep data in ViewModel as long as your apps alive. You can use ViewModel in various ways.
Last but not least, remember makes your Composables stateful, instead of this using state-hositing makes your Composable easier to maintain.
If you build layers top of stateful Composables you increase permutation of states your Composable go into when any MutableState in child or parent changes.
It might be difficult to track state changes from parent to child. I sometimes have this issue when i have Modifier.pointerInput(keys)
with state not changing as expected.
In my opinion state management is one of the most difficult aspects of Compose. Because of this the less stateful Composables you have easier it's to manage your Composable's internal changes
rememberSaveable
then if every state could be stored inViewModels
? What about 2+ViewModels
in 1Composable
? – Metaphrast