Skip to content
tezvyn:

Describe state hoisting in Jetpack Compose.

Source: developer.android.comMediumHow cards are made

Describe state hoisting in Jetpack Compose.

Tests your grasp of unidirectional data flow. Explain state hoisting is moving state up to make composables stateless. This improves reusability, testability, and creates a single source of truth. A red flag is defining it without explaining the benefits.

What's really being asked

Your understanding of a core Compose architectural pattern. The interviewer wants to see if you can articulate not just what state hoisting is, but why it's fundamental to building a scalable, testable, and maintainable Compose UI. They are testing your grasp of unidirectional data flow (UDF), the separation of concerns between stateful and stateless components, and the practical benefits that result.

The full answer

A good answer has three parts. First, define the pattern: State hoisting is moving a composable's state up the component tree to a common ancestor, making the original composable stateless. The state flows down as a parameter, and events (callbacks) flow up to modify the state. Second, explain the primary goal: to create stateless composables. These 'dumb' components are easier to reason about because they only depend on their inputs. Third, list the key benefits: reusability (the component can be used anywhere), testability (it can be tested in isolation by providing input state), and creating a single source of truth (state is owned by one component, preventing inconsistencies).

The mistakes people make

A major red flag is describing state hoisting simply as 'moving state up' without explaining why. This shows a superficial understanding. Another mistake is confusing it with general state management or not being able to articulate the 'events up, state down' flow. Candidates who can't explain how it improves testability or reusability are missing the point of the pattern. Saying 'it's just what you're supposed to do' is a weak answer.

What usually comes next

Expect questions like: 'When would you NOT hoist state?', 'Where should state be hoisted to? A parent composable? A ViewModel?', 'Describe the difference between remember and rememberSaveable in the context of hoisted state.', or 'How does state hoisting relate to unidirectional data flow (UDF)?'.

A concrete example

Imagine a SearchBar composable with an input field. Initially, the text state might be managed inside SearchBar using remember { mutableStateOf("") }. State hoisting means moving this state to the parent screen's composable or its ViewModel. The SearchBar then receives the current text value as a String parameter and a lambda function (String) -> Unit for its onValueChange callback. Now, the SearchBar itself owns no state; it's a dumb component that displays text and reports changes. The parent decides what to do with those changes.

Interview question

What is the primary architectural goal achieved by applying state hoisting in Jetpack Compose?

  • a.To enable direct modification of parent composable state from child composables.
  • b.To allow each composable to manage its own state independently, promoting encapsulation.
  • c.To automatically persist UI state across configuration changes using rememberSaveable.
  • d.To make composables stateless, thereby enhancing their reusability and testability.Correct
Why?

State hoisting's primary goal is to create stateless composables, which significantly improves their reusability and testability by making them 'dumb' components. Option B describes the opposite of state hoisting's intent for the child composable, while Option C refers to state persistence, a related but distinct concern.

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