Why does a Compose screen recompose too much, and how do you fix it?
AndroidSenior
In shortRecomposition runs when state a composable reads changes, and skipping depends on parameters being stable and equal. Over-recomposition usually comes from reading fast-changing state too high up, unstable parameters (mutable classes, plain List from other modules, lambdas capturing unstable values), or new objects created every time. Measure first, then narrow state reads and stabilize parameters.
Model answer
How it works: a composable recomposes when state it read changes. During recomposition, a call can be skipped if all its parameters are equal to last time and considered stable. With strong skipping mode (the default in recent Kotlin Compose compiler versions), unstable parameters are compared by instance, and lambdas are remembered automatically, so most skipping issues now come from new instances and broad state reads.
Common causes:
- Reading fast-changing state high up: reading
scrollState.valueor an animation value in a screen-level composable recomposes the whole screen every frame. Pass lambdas ({ scrollState.value }) and read in layout or draw (Modifier.offset { },graphicsLayer { }). - New objects every recomposition:
listOf(...),Modifierchains with new lambdas or formatters created in the body; hoist orrememberthem. - Unstable models: classes with
varproperties or mutable collections; prefer immutable data classes,kotlinx.collections.immutable, or a stability configuration file for classes from other modules. - Derived values: use
derivedStateOfwhen a value changes less often than its inputs (show a button only whenfirstVisibleItemIndex > 0). - Lazy lists: stable
keys andcontentTypeso items are reused correctly.
Measure: Layout Inspector’s recomposition and skip counts, Compose compiler reports for stability, and Macrobenchmark with JankStats for real frame timing.
What they're looking for
How recomposition and skipping work, stability inference and strong skipping mode, deferring reads to layout or draw, derivedStateOf, keys in lazy lists, and measurement tools.
Common mistakes
- Annotating everything @Stable without making it actually stable.
- Optimizing without measuring recomposition counts.
- Reading scroll or animation state in composition.
More android questions
- How do you make sure background work in a mobile app stops when the user leaves the screen?Mid-level
- What's the difference between a configuration change and process death on Android, and what survives each?Mid-level
- How would you modularize a large Android app, and how do you keep builds fast?Senior
- How do you store an auth token securely on Android?Mid-level