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 THIS TESTS: 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.
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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).
ONE 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") } } }
Read the original → developer.android.com
- #android
- #jetpack compose
- #kotlin
- #state management
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.