State
In Compose you don't change the screen directly. You change state, and Compose runs your composables again to draw the new screen. This page explains how.
Example
@Composable
fun Counter() {
var count by remember { mutableIntStateOf(0) }
Button(onClick = { count++ }) {
Text("Clicked $count times")
}
}

var count by remember { mutableIntStateOf(3) }
Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {
Text("Clicked $count times")
Button(onClick = { count++ }) { Text("Add one") }
}
Recomposition
A composable is a function that describes the screen for the current data. When data it reads changes, Compose calls it again. This is recomposition.
- Only composables that read the changed state run again, not the whole screen.
- A composable can run often, in any order, or be skipped. Don't do work in it that must happen once, like loading data. See Side effects.
- A normal variable doesn't trigger recomposition.
var count = 0inside a composable is reset to 0 every time it runs, and changing it does nothing.
remember and mutableStateOf
Two things make a value work as state:
mutableStateOf(value)makes a value that Compose watches. When you change it, readers recompose.remember { }keeps a value across recompositions. Without it, the state is created anew each time.
var name by remember { mutableStateOf("") } // String, Boolean, any object
var count by remember { mutableIntStateOf(0) } // Int, without boxing. Also Float, Long, Double
val items = remember { mutableStateListOf<String>() } // a list; add/remove update the screen
by lets you read and write the value directly (count++). It needs two imports, which your editor adds: androidx.compose.runtime.getValue and setValue. Without by, use .value: val count = remember { mutableIntStateOf(0) } and count.value++.
Changing an object inside a mutableStateOf doesn't update the screen; only replacing it does. Use immutable data classes and copy(): user = user.copy(name = "Ada").
rememberSaveable
remember forgets when the composable leaves the screen, and when the app is recreated: on Android, when the screen rotates or the system stops the app in the background.
rememberSaveable survives that too. Use it for what the user typed or picked:
var query by rememberSaveable { mutableStateOf("") }
It works with strings, numbers, booleans and other simple values. For your own classes, pass a saver.
State hoisting
Most Material components hold no state. A Checkbox doesn't remember whether it is checked; you tell it with checked, and it tells you about clicks with onCheckedChange:
var agreed by remember { mutableStateOf(false) }
Checkbox(checked = agreed, onCheckedChange = { agreed = it })
Keeping state outside the composable that shows it is state hoisting. Write your own composables the same way: a value and an on...Change callback. The parent keeps the state, so it can share it, save it, or check it.

// The state lives here, so both rows and the summary can use it.
var wifi by rememberSaveable { mutableStateOf(true) }
var bluetooth by rememberSaveable { mutableStateOf(false) }
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
SettingRow("Wi-Fi", wifi, onCheckedChange = { wifi = it })
SettingRow("Bluetooth", bluetooth, onCheckedChange = { bluetooth = it })
Text(
"${listOf(wifi, bluetooth).count { it }} of 2 on",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
/** Stateless: shows the value it gets and reports changes. The caller keeps the state. */
@Composable
fun SettingRow(label: String, checked: Boolean, onCheckedChange: (Boolean) -> Unit) {
Row(Modifier.width(280.dp), verticalAlignment = Alignment.CenterVertically) {
Text(label, Modifier.weight(1f))
Switch(checked = checked, onCheckedChange = onCheckedChange)
}
}
Keep state at the lowest common parent of everything that reads or changes it.
State holders
Bigger components keep their state in an object you create with a remember... function. Create it, pass it in, and read or control the component through it:
| Component | State |
|---|---|
LazyColumn |
rememberLazyListState(): scroll position, animateScrollToItem |
Modifier.verticalScroll |
rememberScrollState() |
ModalNavigationDrawer |
rememberDrawerState(DrawerValue.Closed): open(), close() |
ModalBottomSheet |
rememberBottomSheetState(...): show(), hide() |
TextField |
rememberTextFieldState(): the text and cursor |
HorizontalPager |
rememberPagerState { pageCount }: current page |
SnackbarHost |
remember { SnackbarHostState() }: showSnackbar(...) |
DatePicker |
rememberDatePickerState(): the picked date |
Their actions, like drawerState.open() or listState.animateScrollToItem(0), are suspend functions. See the next section.
Side effects
Some work must not run on every recomposition: loading data, starting an animation, showing a snackbar. Compose has effects for that:
LaunchedEffect(key) { }: runs a coroutine when the composable appears, and again whenkeychanges. Cancelled when it leaves.rememberCoroutineScope(): a scope to launch coroutines from a click, likescope.launch { drawerState.open() }.DisposableEffect(key) { onDispose { } }: set something up, and clean it up when the composable leaves.
val scope = rememberCoroutineScope()
Button(onClick = { scope.launch { listState.animateScrollToItem(0) } }) { Text("Top") }
LaunchedEffect(userId) {
user = repository.load(userId) // runs again only when userId changes
}
Derived state
When a value is computed from state that changes often, derivedStateOf makes readers recompose only when the result changes:
val showButton by remember { derivedStateOf { listState.firstVisibleItemIndex > 0 } }
Without it, everything reading showButton would recompose on every scrolled pixel.
State from outside Compose
App data often lives in a class outside the UI, like a ViewModel, as a Kotlin StateFlow. Turn it into Compose state with collectAsState():
val uiState by viewModel.uiState.collectAsState()
On Android, prefer collectAsStateWithLifecycle() from androidx.lifecycle:lifecycle-runtime-compose, which also exists for Compose Multiplatform. It stops collecting while the app is in the background.
Summary
| You want | Write |
|---|---|
| A value that updates the screen | var x by remember { mutableStateOf(...) } |
| Keep it through rotation | rememberSaveable { mutableStateOf(...) } |
| A list that updates the screen | remember { mutableStateListOf<T>() } |
| Let the parent own the state | parameters value and onValueChange |
| Call a suspend function on click | val scope = rememberCoroutineScope() + scope.launch { } |
| Run once when shown | LaunchedEffect(Unit) { } |
| Compute from fast-changing state | remember { derivedStateOf { ... } } |
| Data from a ViewModel | flow.collectAsState() |