How does Compose layout work, and how do you keep custom UI and animations efficient?
AndroidSenior
In shortCompose lays out in one pass where each child is measured once (constraints down, sizes up, then placement), and work happens in three phases (composition, layout, drawing). Use intrinsics instead of double measurement, avoid subcomposition in list items, and read fast-changing state such as animations in the layout or draw phase.
Model answer
Layout model: a parent passes Constraints to each child, measures each child exactly once (measuring twice throws), then places them. That guarantees linear layout time even in deep trees. Custom arrangements use Layout { measurables, constraints -> layout(w, h) { place(...) } }.
When children depend on each other: use intrinsic measurements (IntrinsicSize.Min, maxIntrinsicHeight), which ask for a size without measuring, at the cost of an extra traversal. SubcomposeLayout and BoxWithConstraints compose content during layout when content depends on available space; that composition can’t be skipped, so keep them out of list items.
Phases: composition (what to show), layout (where), drawing (how). A state read in a phase invalidates only that phase and later ones. So fast-changing values, such as animations, scroll offsets and drag positions, should be read in lambda modifiers: Modifier.offset { IntOffset(x.roundToPx(), 0) } (layout) or Modifier.graphicsLayer { alpha = a } and drawBehind { } (draw), not Modifier.offset(x = …) or alpha(a), which recompose every frame.
Also: reusable modifiers as Modifier.Node (not composed {}), stable keys in lazy lists, and checking recomposition counts in Layout Inspector before and after.
What they're looking for
The single-pass, measure-once model, intrinsics, the cost of SubcomposeLayout, the three phases and deferred state reads, and tools to verify (Layout Inspector recomposition counts).
Common mistakes
- Measuring children twice in a custom Layout.
- Reading animated values in composition with Modifier.alpha or offset(x = …).
- BoxWithConstraints in every list item.
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