Predict the output
The user taps the button once. Which composables run again?
@Composable
fun Screen() {
var count by remember { mutableStateOf(0) }
Column {
Header(title = "Cart")
Button(onClick = { count++ }) { Text("Add") }
Text("Items: $count")
}
}
@Composable
fun Header(title: String) { Text(title) }Hint 1 of 2
Where is count read?
Hint 2 of 2
Did Header's parameter change?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Screen's body runs again, Header is skipped
Correct. Screen read count, so it re-runs; Header's parameter didn't change, so it's skipped.
Why not the others?
- B Only the Text that shows the count. count is read in Screen's body (to build the string), so Screen's scope re-runs.
- C Everything, including Header. Header gets the same title, so Compose skips it.
- D Nothing; you must call invalidate(). Changing a State is enough.
Illustrative, based on Compose and Android rules; not a device recording
Text version
| Composable | count read in Screen | count read in a child |
|---|---|---|
Screen | 1 recomposition | 0: skipped |
Header | 0: skipped | 0: skipped |
Button | 0: skipped | 0: skipped |
Text | 1 recomposition | 1 recomposition |