Challenge - 5 Problems
SavedStateHandle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
SavedStateHandle preserves UI state on configuration change
Consider a ViewModel using SavedStateHandle to store a counter value. What will be the value of
counter after a device rotation if the user incremented it to 5 before rotation?Android Kotlin
class CounterViewModel(private val state: SavedStateHandle) : ViewModel() { var counter: Int get() = state.get<Int>("counter") ?: 0 set(value) = state.set("counter", value) } // User increments counter to 5, then rotates device.
Attempts:
2 left
💡 Hint
SavedStateHandle keeps data across configuration changes like rotation.
✗ Incorrect
SavedStateHandle stores data in a bundle that survives configuration changes, so the counter value 5 is preserved after rotation.
❓ lifecycle
intermediate1:30remaining
When is SavedStateHandle data cleared?
At what point is the data stored in SavedStateHandle cleared automatically by the Android system?
Attempts:
2 left
💡 Hint
Think about the lifecycle of ViewModel and when it is destroyed.
✗ Incorrect
SavedStateHandle data is cleared when the ViewModel is cleared, which happens when the UI is permanently destroyed, not on rotation or temporary events.
📝 Syntax
advanced1:30remaining
Correct way to retrieve a nullable value from SavedStateHandle
Which option correctly retrieves a nullable String value named "username" from SavedStateHandle in Kotlin?
Android Kotlin
val username: String? = savedStateHandle.???
Attempts:
2 left
💡 Hint
Check the official SavedStateHandle API for retrieving values.
✗ Incorrect
The method get(key) returns a nullable value of type T or null if not present. Other methods do not exist or have different signatures.
🔧 Debug
advanced2:00remaining
Why does this SavedStateHandle update not persist after process death?
Given this code snippet, why does the updated value not persist after the app process is killed and restarted?
savedStateHandle["score"] = 10 // app is killed and restarted val score = savedStateHandle.get("score") ?: 0
Attempts:
2 left
💡 Hint
Think about what SavedStateHandle is designed to save and what it is not.
✗ Incorrect
SavedStateHandle saves data to survive configuration changes but does not persist data across process death. For that, you need persistent storage.
🧠 Conceptual
expert2:30remaining
How does SavedStateHandle integrate with Jetpack Navigation component?
Which statement best describes how SavedStateHandle works with Jetpack Navigation to pass and save UI state between destinations?
Attempts:
2 left
💡 Hint
Consider how navigation arguments and SavedStateHandle share data.
✗ Incorrect
Jetpack Navigation integrates with SavedStateHandle to automatically save and restore navigation arguments and UI state across configuration changes and process death (if saved in saved state registry).