Why some people still use viewmodel in jetpack compose?

I watch some tutorial projects on YouTube and some of them make viewmodel file and write some bunch of code, just to use viewmodel!

We have remember and rememberSavable and state and recomposition and ... in jetpack compose. so why still some people use viewmodel?! it's some more code and makes the project more difficult to understand for we beginners.

I guess they learned viewmodel for XML hardly and they can't just forget about it!

isn't it possible in jetpack compose to write the code in a way that we don't need to use viewmodel anyway?


  • 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