State and Recomposition
Android Development Fundamentals with Compose
Course 4 ยท Chapter 4 ยท Why the Screen Changes
This is the most important idea in Compose. In XML you change the screen by finding a view and telling it what to show. In Compose you never touch the UI directly: you change state (a value your composables read), and Compose recomposes, that is, calls the affected composables again so the screen matches the new value. Master this and everything else in Compose follows from it.
remember and mutableStateOf, recomposition triggered by writing state, why remember is needed, rememberSaveable and configuration changes, and state hoisting with stateless and stateful composables.
Thin in the docs I found: mutableStateListOf (only a performance note). General knowledge, not checked: the details of how the compiler tracks reads, and the list example. All code is unrun (no Kotlin compiler on the machine used to write this); the import lines are given so you can check them. Nothing was seen on your install.
State, and the Bug Without It
Here is the mistake almost every beginner makes. The variable changes when you tap, but the screen does not, because nothing told Compose to look:
(docs) A local mutableStateOf without remember is recreated on every pass, because recomposition can happen at any time and calls the composable again, resetting the value. The fix has two parts:
mutableStateOf makes a value Compose can watch, and remember keeps it between recompositions.
| Piece | What it does |
|---|---|
mutableIntStateOf(0) | Creates an observable Int holder starting at 0 (general knowledge: the primitive version of mutableStateOf). |
remember { ... } | (docs) Stores the object in memory in the composition and forgets it when the composable leaves the screen. |
by (with getValue, setValue) | Lets you write count and count++ instead of count.value. |
count++ | (docs) Writing to the state schedules recomposition of every composable that read it. |
What Recomposition Is
When state changes, Compose calls again the composables that read it (docs), and only those (general knowledge). Your composable is therefore a function from state to UI: same state, same screen. Two consequences:
- A composable may run many times, and at any time, so keep it free of slow work and surprises. Chapters 5 onward show where events and side effects belong.
rememberlasts while the composable stays on screen. To keep a value across rotation you userememberSaveable(docs: it saves the state in a Bundle), as in Chapter 2.
State Hoisting: Stateless and Stateful
(docs) State hoisting means moving state up to the caller so the composable below becomes stateless: it receives a value and a callback instead of owning the state. The shape is always: value down, events up.
| Stateless | Stateful | |
|---|---|---|
| Owns the state? | No: it receives the value and a callback | Yes: it holds remembered state |
| Reuse | Easy: any caller can supply the state | Harder |
| Preview and test | Easy: pass any value | Awkward |
| Typical home | Most of your composables | A screen or a few top-level pieces |
Prefer stateless composables and keep the state in as few places as possible (general knowledge, and the usual Compose advice). The two counters in Exercise 2 show why: when one parent owns both values it can also show their total.
Lists in State
(general knowledge; the docs I found said little about it) To change a list on screen you need a list Compose can watch. mutableStateListOf gives one whose changes (add, remove) trigger recomposition. The one source on it that I found
notes that it tracks each element's changes individually, so it has a cost; Chapter 6 returns to lists and LazyColumn.
Compared with the XML Way
| Views (Android Development Fundamentals) | Compose | |
|---|---|---|
| Update the screen | Find the view and call setText, setVisibility | Change state; the UI is recomposed |
| Source of truth | Spread between views and variables | The state; the UI is derived from it |
| Forgetting to update a view | A common bug: the screen shows old data | Not possible for state-driven UI; the common bug is a plain variable that is not state |
| Keep across rotation | onSaveInstanceState | rememberSaveable |
Try It Yourself
Type in BrokenCounter and run it. Describe what happens when you tap. Then fix it with remember and mutableIntStateOf, and explain in a sentence why the first version failed.
Using the stateless CounterButton, build TwoCounters: two independent counters in one parent that also shows their total. Say which composable owns the state.
Make a Switch that shows the text "On" or "Off", keeping its value across rotation. Then switch rememberSaveable to remember and rotate the device to see the difference.
remember and mutableStateOf; that writing state schedules recomposition; that remember forgets when the composable leaves the composition; rememberSaveable and Bundles; state hoisting with stateless and stateful composables; the note that mutableStateListOf tracks elements individually.Not verified: that only affected composables rerun; how
mutableStateListOf is meant to be used (not found); mutableIntStateOf as the primitive form (general knowledge); the "prefer stateless" advice (general knowledge); the XML comparison column; the imports; every listing and solution (unrun); the exercises (not run by me). Nothing was checked on your install.Sources: State and Jetpack Compose (developer.android.com), State in Jetpack Compose (developer.android.com codelab), Intro to state in Compose (developer.android.com codelab).
TextField input, simple gestures, Intents and passing data to another screen. It builds directly on the state you met here.