Jetpack Compose State Management Patterns That Prevent Unnecessary Recomposition
Lagging lists and dropped frames in Jetpack Compose almost always come from unstable state objects or reading state too high in the tree. Here is how to keep Compose screens at 60fps.
Jetpack Compose lets Android teams build clean, declarative UIs in half the code required by legacy XML layouts. However, when a screen grows complex—with search bars, filter chips, timers, and scrollable cards—poor state placement can cause the entire screen to recompose on every keystroke.
1. Model Each Screen with One Immutable UiState Data Class
Avoid scattering eight separate MutableStateFlow properties across a ViewModel. Group them into a single immutable data class exposed via StateFlow<ScreenUiState>:
@Immutable
data class InventoryUiState(
val isLoading: Boolean = false,
val query: String = "",
val items: List<ProductItem> = emptyList(),
val errorMessage: String? = null
)
Because Kotlin's standard List interface is treated as unstable by the Compose compiler (since it could be backed by a MutableList), mark immutable UI models with @Immutable or use kotlinx.collections.immutable.ImmutableList so Compose can skip recomposing unchanged rows.
2. Always Provide Stable Keys in LazyColumn
Never rely on positional indexes for dynamic lists. Always pass the item's database ID as the key and specify contentType when mixing headers and cards:
LazyColumn {
items(
items = state.items,
key = { it.id },
contentType = { "product_row" }
) { product ->
ProductRow(
product = product,
onSelect = { onProductClick(product.id) }
)
}
}
3. Defer State Reads with Lambdas
If you animate a scroll offset or countdown value, reading that state at the top of the Composable forces every child inside it to recompose every frame. Pass a lambda (() -> Float) into modifiers like Modifier.graphicsLayer { translationY = offsetProvider() } so Compose skips composition and layout entirely, updating only the draw phase.
4. Verify with Layout Inspector
Before shipping any Compose screen, open Android Studio's Layout Inspector and enable recomposition counts. If typing one character in a search box increments the recomposition count on your bottom navigation bar or static header, you have a state leak to isolate.
Need help architecting a Laravel backend, Kotlin Android app, or joining our weekend engineering cohort?