Learning Android Development

Managing Compose State Variable With And Without ViewModel

Decide where can you put your Compose State variable

Photo by Jake Young on Unsplash

In Jetpack Compose, the way we update the UI is by updating the State Variable. But where should we put the Compose State Variable? How can we get it to survive per state restoration?

Here I’m sharing the different places of storing Compose State Variable, starting from the very basic.

In case you need some basic about Jetpack Compose State Variable, refers to the below articl

1. In the Activity Itself (not recommended)

We can have the most old-school way to store State Variable in the Activity.

class MainActivity : ComponentActivity() {

private var stateVariable by mutableStateOf(0)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
FlowExperimentTheme {
Surface(color =…

--

--