Skip to content
tezvyn:

Explain Recomposition in Jetpack Compose

Source: developer.android.comMediumHow cards are made

Explain Recomposition in Jetpack Compose

Tests your grasp of Compose's declarative model. A good answer defines recomposition, explains state-read triggers, and details how stability and positional memoization enable skipping. A red flag is assuming any state change redraws the entire UI.

What's really being asked

This question tests your fundamental understanding of the declarative UI paradigm, which is the core of Jetpack Compose. The interviewer isn't looking for a textbook definition. They want to know if you understand the performance model: why Compose is efficient and how to write code that leverages its strengths. It's a test of your knowledge of state management, compiler optimizations, and performance best practices.

The full answer

First, define recomposition as the process of re-executing composable functions to update the UI when their underlying state changes. Contrast this with the imperative view system's manual invalidation.

Second, explain the trigger mechanism. When a composable function reads a State<T> object, it implicitly subscribes to updates for that state. Any write to that State object schedules a recomposition for all its subscribed readers.

Third, detail the primary optimization: skipping. The Compose compiler analyzes the parameters of your composables. If all parameters are 'stable' (unchanged since the last execution), Compose can entirely skip re-running the function and its children. Stability is key; primitives, strings, and classes marked with @Immutable are stable. Unstable types like a standard List can break this optimization.

Fourth, mention positional memoization. Compose identifies and tracks composables by their call site (source code location and parent structure), not by creating and comparing object instances. This allows it to intelligently update or skip specific sub-trees without affecting sibling composables.

The mistakes people make

A major red flag is stating that recomposition redraws the whole screen or a large portion of the UI on every state change. This demonstrates a fundamental misunderstanding of Compose's efficiency model. Another common mistake is confusing recomposition with the old View system's inflation and measure/layout/draw pass; they are very different. A senior candidate should also avoid hand-waving and calling it 'magic'—they should be able to explain the role of stability and the compiler.

What usually comes next

Expect questions like: "What makes a data class unstable, and how would you fix it?" (e.g., using a var instead of a val, or a mutable collection). "How would you debug excessive or unexpected recompositions?" (Using the Layout Inspector in Android Studio). "When should you use derivedStateOf?" (To limit recomposition when state is calculated from other state and changes less frequently).

A concrete example

Consider a simple screen with a counter. You have a count variable stored in mutableStateOf(0). A Text composable displays the count value, and a Button composable has an onClick lambda that increments it. When the user taps the button, the count state is updated. Because the Text composable reads count, it is scheduled for recomposition. However, the Button composable itself does not recompose, because its input parameters (the onClick lambda) are stable and have not changed. Compose is smart enough to re-execute only the Text function, making the update extremely efficient.

Interview question

Which statement accurately describes how Jetpack Compose primarily optimizes recomposition to avoid unnecessary UI updates?

  • a.It automatically redraws only the affected pixels on the screen, leaving unchanged areas untouched.
  • b.It skips the execution of a composable function and its children if all its input parameters are stable and have not changed since the last execution.Correct
  • c.It performs a deep comparison of the entire UI tree to find the smallest possible changes.
  • d.It re-executes only the composable functions that explicitly declare their need for updates using remember blocks.
Why?

The card states that Compose's primary optimization is 'skipping': if all parameters are 'stable' and unchanged, Compose skips re-running the function and its children. Option C describes a general diffing algorithm, which is not Compose's core mechanism for skipping function execution.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles