androidengineers.Book a session

Android Basics & UI (Jetpack Compose)

State Management in Compose

article50 minHard

State is any value that can change over time. In Compose, state controls what the UI displays.

Examples of state:

  • text typed into a field
  • whether a dialog is visible
  • loading, success, or error state
  • selected tab
  • list of items from an API

Remember

Use remember for local UI state that should survive recomposition.

@Composable
fun Counter() {
    var count by remember { mutableStateOf(0) }

    Button(onClick = { count++ }) {
        Text("Clicked $count times")
    }
}

Without remember, the value would reset during recomposition.

State Hoisting

State hoisting means moving state up and passing values and events down.

@Composable
fun NameInput(
    name: String,
    onNameChange: (String) -> Unit
) {
    TextField(
        value = name,
        onValueChange = onNameChange
    )
}

This makes the composable reusable and easier to test.

ViewModel State

For screen-level state, use a ViewModel.

data class LoginUiState(
    val email: String = "",
    val password: String = "",
    val isLoading: Boolean = false
)

The ViewModel owns the state. The composable observes it and sends events.

Avoid These Mistakes

MistakeBetter approach
Storing business logic in composablesPut it in ViewModel/use cases
Duplicating the same state in multiple placesHave one source of truth
Mutating lists directlyCreate a new list
Using remember for data that must survive process deathUse ViewModel and saved state

Practice

Build a small login form with email and password fields. Keep the text state in the parent composable and pass it down to reusable input composables.

Summary

Compose UI is a function of state. Keep local state local, hoist reusable state, and use ViewModels for screen-level behavior.

YOUR LEARNING JOURNEY

0 of 22 available lessons completed

Progress saved in this browser. No account needed.
State Management in Compose | Junior Android Developer | Android Engineers