What if your app could magically remember everything you typed, even if you turn your phone sideways?
Why rememberSaveable for configuration changes in Android Kotlin? - Purpose & Use Cases
Imagine you are filling out a form in an app on your phone. Suddenly, you rotate your device from portrait to landscape. Without special handling, all the information you typed disappears, and you have to start over.
Manually saving and restoring data during device rotation is tricky and error-prone. You have to write extra code to save state, restore it, and handle many edge cases. This slows development and can cause bugs where users lose their input.
The rememberSaveable function automatically saves your UI state across configuration changes like rotation. It remembers values and restores them without extra boilerplate, making your app feel smooth and reliable.
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("input", inputText)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
inputText = savedInstanceState.getString("input") ?: ""
}var inputText by rememberSaveable { mutableStateOf("") }You can build apps that keep user input and UI state seamlessly, even when the device rotates or configuration changes happen.
Think of a messaging app where you type a long message. If you rotate your phone accidentally, your typed message stays intact without losing any words.
rememberSaveable keeps UI state across configuration changes automatically.
It reduces manual code and bugs related to saving/restoring state.
It improves user experience by preserving input and UI smoothly.
Practice
rememberSaveable in Jetpack Compose?Solution
Step 1: Understand configuration changes
Configuration changes like screen rotation cause the UI to be recreated, losing normal state.Step 2: Role of rememberSaveable
rememberSaveableautomatically saves and restores simple UI state during these changes.Final Answer:
To save UI state across configuration changes like screen rotations -> Option AQuick Check:
rememberSaveable = saves state on config changes [OK]
- Confusing rememberSaveable with permanent storage
- Thinking it manages animations or network
- Using remember instead and losing state on rotation
rememberSaveable?Solution
Step 1: Identify correct usage of rememberSaveable
The correct syntax uses delegation withbyand initializes withmutableStateOfinside a lambda.Step 2: Check each option
var count by rememberSaveable { mutableStateOf(0) } matches the correct pattern:var count by rememberSaveable { mutableStateOf(0) }.Final Answer:
var count by rememberSaveable { mutableStateOf(0) } -> Option CQuick Check:
rememberSaveable needs mutableStateOf inside lambda [OK]
- Using remember instead of rememberSaveable
- Missing delegation with 'by'
- Passing initial value directly without lambda
var count by rememberSaveable { mutableStateOf(0) }
Button(onClick = { count++ }) {
Text("Count: $count")
}Solution
Step 1: Understand rememberSaveable behavior
It saves and restores the state value across configuration changes like rotations.Step 2: Analyze the code
Count increments on button clicks and is preserved after rotations, so it does not reset.Final Answer:
Count value is preserved across rotations -> Option DQuick Check:
rememberSaveable preserves state on rotation [OK]
- Assuming count resets after rotation
- Thinking rotation increments count
- Believing app crashes due to state loss
val isChecked = remember { mutableStateOf(false) }
Checkbox(checked = isChecked.value, onCheckedChange = { isChecked.value = it })Solution
Step 1: Identify state preservation method
Usingrememberalone does not save state across configuration changes.Step 2: Correct approach
ReplacerememberwithrememberSaveableto preserve toggle state on rotation.Final Answer:
Using remember instead of rememberSaveable loses state on rotation -> Option BQuick Check:
rememberSaveable needed for state preservation [OK]
- Confusing remember with rememberSaveable
- Thinking mutableStateOf is wrong for checkbox
- Missing onCheckedChange lambda
rememberSaveable with a custom saver for a complex data type?Solution
Step 1: Understand saving complex types
Complex types like TextFieldValue need a custom saver to be saved/restored by rememberSaveable.Step 2: Check correct syntax
var userInput by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue("") ) } usesstateSaver = TextFieldValue.Saverwith rememberSaveable and mutableStateOf, which is correct.Final Answer:
var userInput by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue("") ) } -> Option AQuick Check:
Custom saver needed for complex types with rememberSaveable [OK]
- Not providing a custom saver for complex types
- Using remember instead of rememberSaveable
- Passing initial value directly without lambda
Ready to prove it?
Pass the test and this pattern moves unknown
