0
0
Android Kotlinmobile~20 mins

SavedStateHandle in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SavedStateHandle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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.
Acounter will increment automatically to 6 after rotation
Bcounter will remain 5 after rotation
Ccounter will be null after rotation causing crash
Dcounter will reset to 0 after rotation
Attempts:
2 left
💡 Hint
SavedStateHandle keeps data across configuration changes like rotation.
lifecycle
intermediate
1:30remaining
When is SavedStateHandle data cleared?
At what point is the data stored in SavedStateHandle cleared automatically by the Android system?
AWhen the ViewModel is cleared after the associated UI is destroyed permanently
BWhen the app process is killed by the system
CWhen the device is rotated
DWhen the user presses the back button once
Attempts:
2 left
💡 Hint
Think about the lifecycle of ViewModel and when it is destroyed.
📝 Syntax
advanced
1: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.???
AgetOrDefault("username", "")
BgetValue("username")
Cget<String>("username")
DgetNullable("username")
Attempts:
2 left
💡 Hint
Check the official SavedStateHandle API for retrieving values.
🔧 Debug
advanced
2: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
AThe value 10 is not serializable and cannot be saved
BThe ViewModel was not recreated properly after restart
CThe key "score" is invalid and cannot be saved
DSavedStateHandle only saves data during configuration changes, not process death
Attempts:
2 left
💡 Hint
Think about what SavedStateHandle is designed to save and what it is not.
🧠 Conceptual
expert
2: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?
ASavedStateHandle automatically saves and restores navigation arguments and state when navigating between destinations
BSavedStateHandle requires manual serialization of navigation arguments to bundles before navigation
CSavedStateHandle cannot be used with Jetpack Navigation and is only for ViewModel internal state
DSavedStateHandle only works with fragments, not with navigation components
Attempts:
2 left
💡 Hint
Consider how navigation arguments and SavedStateHandle share data.