Skip to content
tezvyn:

What is the purpose of `remember` in Jetpack Compose?

Source: developer.android.comEasyHow cards are made

What is the purpose of `remember` in Jetpack Compose?

This tests your grasp of Compose's recomposition lifecycle. A good answer explains remember caches an object across recompositions, preventing state loss. It's used with mutableStateOf to hold the same state instance.

What's really being asked

This question tests your fundamental understanding of the Compose runtime model. The interviewer wants to see if you grasp that composable functions are stateless and can be re-executed (recomposed) at any time. Your answer must show you understand why local variables are insufficient for holding state and how remember solves this core problem.

The full answer

First, explain that composables are just functions that get re-invoked to update the UI. Second, point out that because they are re-invoked, any normal local variable inside them gets re-initialized, losing its previous value. Third, introduce remember as the mechanism to solve this. It stores the result of its lambda in the Composition itself, associated with the call site. On subsequent recompositions, remember returns the stored value instead of re-running the lambda. Finally, connect this to mutableStateOf. You remember the MutableState object so that you're always interacting with the same state holder instance across recompositions, not a new one each time.

The mistakes people make

One major red flag is confusing remember with rememberSaveable. remember only preserves state across recompositions. It does NOT survive configuration changes (like screen rotation) or process death. rememberSaveable does, by saving the state to a Bundle. A senior candidate is expected to know this distinction clearly. Another weak answer is simply describing the syntax without explaining the 'why'. Saying "you use it to create state" is too shallow; the key is explaining that it preserves the state object across function calls.

What usually comes next

Expect follow-ups like: "When would you use rememberSaveable instead?" to test knowledge of configuration changes. Or, "What is the purpose of passing a key to remember, like remember(userId) { ... }?" to check if you understand how to invalidate and recalculate a remembered value when its dependencies change. A deeper question for a senior role might be "How does remember work internally?" (Answer: It uses the compiler plugin to store values in the composition's slot table).

A concrete example

Here is a simple counter. Without remember, every time you clicked the button, count would be reset to 0 during the resulting recomposition, and the text would never update past 1.

@Composable fun Counter() { // remember stores the MutableState instance across recompositions.

var count by remember { mutableStateOf(0) }

Column {

Text(text = "You have clicked the button $count times.")
Button(onClick = { count++ }) {
Text("Click me")
}
}
}

Interview question

If a `mutableStateOf` variable is declared inside a composable without `remember`, what happens when the UI recomposes?

  • a.The UI updates correctly, but with slightly worse performance due to object recreation.
  • b.The app will crash because state must be saveable across configuration changes.
  • c.The state is preserved automatically by the Compose runtime within the function's scope.
  • d.The variable is reset to its initial value, losing any previous updates.Correct
Why?

Composable functions are re-invoked on recomposition, so any local variable is re-initialized. `remember` caches the state object in the Composition, preventing it from being reset. Without it, the state is lost.

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