Bird
Raised Fist0
Android Kotlinmobile~15 mins

rememberSaveable for configuration changes in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: RememberSaveable Demo
This screen shows a counter that keeps its value even when the device is rotated or configuration changes happen.
Target UI
---------------------
| RememberSaveable  |
| Demo Screen       |
|                   |
| Count: 0          |
|                   |
| [Increment]       |
---------------------
Display a count starting at 0
Add a button labeled 'Increment' that increases the count by 1 when tapped
Use rememberSaveable to keep the count value across configuration changes like screen rotation
Starter Code
Android Kotlin
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun RememberSaveableDemo() {
    // TODO: Add rememberSaveable state for count
    // TODO: Display count and button
}

@Preview(showBackground = true)
@Composable
fun PreviewRememberSaveableDemo() {
    RememberSaveableDemo()
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun RememberSaveableDemo() {
    var count by rememberSaveable { mutableStateOf(0) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.Center
    ) {
        Text(text = "Count: $count", style = MaterialTheme.typography.headlineMedium)
        Spacer(modifier = Modifier.height(16.dp))
        Button(onClick = { count++ }) {
            Text("Increment")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewRememberSaveableDemo() {
    RememberSaveableDemo()
}

We use rememberSaveable to create a state variable count that survives configuration changes like screen rotations. This means when the user rotates the device, the count value does not reset to zero.

The UI shows the current count and a button labeled 'Increment'. When the button is tapped, the count increases by 1 and the UI updates automatically.

The Column arranges the text and button vertically with some spacing for a clean layout.

Final Result
Completed Screen
---------------------
| RememberSaveable  |
| Demo Screen       |
|                   |
| Count: 5          |
|                   |
| [Increment]       |
---------------------
Tapping 'Increment' increases the count number by 1
Rotating the device keeps the count value unchanged
Stretch Goal
Add a 'Reset' button that sets the count back to zero
💡 Hint
Add another Button below the Increment button with onClick resetting count to 0

Practice

(1/5)
1. What is the main purpose of rememberSaveable in Jetpack Compose?
easy
A. To save UI state across configuration changes like screen rotations
B. To permanently store data in a database
C. To create animations in the UI
D. To manage network requests efficiently

Solution

  1. Step 1: Understand configuration changes

    Configuration changes like screen rotation cause the UI to be recreated, losing normal state.
  2. Step 2: Role of rememberSaveable

    rememberSaveable automatically saves and restores simple UI state during these changes.
  3. Final Answer:

    To save UI state across configuration changes like screen rotations -> Option A
  4. Quick Check:

    rememberSaveable = saves state on config changes [OK]
Hint: RememberSaveable = remember + auto save/restore state [OK]
Common Mistakes:
  • Confusing rememberSaveable with permanent storage
  • Thinking it manages animations or network
  • Using remember instead and losing state on rotation
2. Which of the following is the correct syntax to declare a counter state that survives configuration changes using rememberSaveable?
easy
A. var count = mutableStateOf(0)
B. val count = remember { mutableStateOf(0) }
C. var count by rememberSaveable { mutableStateOf(0) }
D. val count by rememberSaveable(0)

Solution

  1. Step 1: Identify correct usage of rememberSaveable

    The correct syntax uses delegation with by and initializes with mutableStateOf inside a lambda.
  2. Step 2: Check each option

    var count by rememberSaveable { mutableStateOf(0) } matches the correct pattern: var count by rememberSaveable { mutableStateOf(0) }.
  3. Final Answer:

    var count by rememberSaveable { mutableStateOf(0) } -> Option C
  4. Quick Check:

    rememberSaveable needs mutableStateOf inside lambda [OK]
Hint: Use var count by rememberSaveable { mutableStateOf(0) } [OK]
Common Mistakes:
  • Using remember instead of rememberSaveable
  • Missing delegation with 'by'
  • Passing initial value directly without lambda
3. Given this code snippet, what will be the displayed count after rotating the device twice?
var count by rememberSaveable { mutableStateOf(0) }
Button(onClick = { count++ }) {
  Text("Count: $count")
}
medium
A. App crashes on rotation due to state loss
B. Count resets to 0 after each rotation
C. Count increments automatically on rotation
D. Count value is preserved across rotations

Solution

  1. Step 1: Understand rememberSaveable behavior

    It saves and restores the state value across configuration changes like rotations.
  2. Step 2: Analyze the code

    Count increments on button clicks and is preserved after rotations, so it does not reset.
  3. Final Answer:

    Count value is preserved across rotations -> Option D
  4. Quick Check:

    rememberSaveable preserves state on rotation [OK]
Hint: rememberSaveable keeps count after rotations [OK]
Common Mistakes:
  • Assuming count resets after rotation
  • Thinking rotation increments count
  • Believing app crashes due to state loss
4. What is wrong with this code if the goal is to preserve the toggle state across configuration changes?
val isChecked = remember { mutableStateOf(false) }
Checkbox(checked = isChecked.value, onCheckedChange = { isChecked.value = it })
medium
A. The onCheckedChange lambda is missing
B. Using remember instead of rememberSaveable loses state on rotation
C. Checkbox requires a String value, not Boolean
D. MutableStateOf should not be used for checkboxes

Solution

  1. Step 1: Identify state preservation method

    Using remember alone does not save state across configuration changes.
  2. Step 2: Correct approach

    Replace remember with rememberSaveable to preserve toggle state on rotation.
  3. Final Answer:

    Using remember instead of rememberSaveable loses state on rotation -> Option B
  4. Quick Check:

    rememberSaveable needed for state preservation [OK]
Hint: Use rememberSaveable, not remember, for config change state [OK]
Common Mistakes:
  • Confusing remember with rememberSaveable
  • Thinking mutableStateOf is wrong for checkbox
  • Missing onCheckedChange lambda
5. You want to save a user input text that should survive configuration changes and process death. Which approach correctly uses rememberSaveable with a custom saver for a complex data type?
hard
A. var userInput by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue("") ) }
B. var userInput by rememberSaveable { mutableStateOf(TextFieldValue("") ) }
C. var userInput by remember { mutableStateOf(TextFieldValue("") ) }
D. var userInput by rememberSaveable(TextFieldValue("") )

Solution

  1. Step 1: Understand saving complex types

    Complex types like TextFieldValue need a custom saver to be saved/restored by rememberSaveable.
  2. Step 2: Check correct syntax

    var userInput by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue("") ) } uses stateSaver = TextFieldValue.Saver with rememberSaveable and mutableStateOf, which is correct.
  3. Final Answer:

    var userInput by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue("") ) } -> Option A
  4. Quick Check:

    Custom saver needed for complex types with rememberSaveable [OK]
Hint: Use stateSaver parameter for complex types in rememberSaveable [OK]
Common Mistakes:
  • Not providing a custom saver for complex types
  • Using remember instead of rememberSaveable
  • Passing initial value directly without lambda
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard