We use rememberSaveable to keep data safe when the screen changes, like rotating the phone. It helps keep your app's state without losing it.
rememberSaveable for configuration changes in Android Kotlin
val state = rememberSaveable { mutableStateOf(initialValue) }rememberSaveable works like remember but also saves state across configuration changes.
You can use it with simple types like Int, String, Boolean, or with custom types if you provide a saver.
val count = rememberSaveable { mutableStateOf(0) }val name = rememberSaveable { mutableStateOf("") }val isChecked = rememberSaveable { mutableStateOf(false) }This app has a text input and a button showing how many times it was clicked. Both the typed name and click count stay the same even if you rotate the device.
import androidx.compose.foundation.layout.Column import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.rememberSaveable import androidx.compose.ui.tooling.preview.Preview @Composable fun RememberSaveableExample() { val name = rememberSaveable { mutableStateOf("") } val count = rememberSaveable { mutableStateOf(0) } Column { TextField( value = name.value, onValueChange = { name.value = it }, label = { Text("Enter your name") } ) Button(onClick = { count.value++ }) { Text("Clicks: ${count.value}") } } } @Preview @Composable fun PreviewRememberSaveableExample() { RememberSaveableExample() }
rememberSaveable automatically saves simple types like Int, String, and Boolean.
For complex objects, you need to provide a custom saver to rememberSaveable.
This helps avoid losing user data during common changes like screen rotation or language change.
rememberSaveable keeps your UI state safe during configuration changes.
Use it for user inputs, counters, toggles, and other simple states.
It works like remember but adds automatic saving and restoring.
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
