Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to retrieve a value from SavedStateHandle.
Android Kotlin
val name: String? = savedStateHandle.get<String>([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the key string.
Using a variable name without quotes.
✗ Incorrect
The key for SavedStateHandle must be a string literal, so it needs quotes.
2fill in blank
mediumComplete the code to save a value into SavedStateHandle.
Android Kotlin
savedStateHandle.set([1], "John")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys when calling set().
Confusing variable names with string keys.
✗ Incorrect
The key must be a string literal with quotes when saving a value.
3fill in blank
hardFix the error in the code to observe a LiveData from SavedStateHandle.
Android Kotlin
val liveData = savedStateHandle.getLiveData<String>([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys causing compile errors.
Mixing variable names with string keys.
✗ Incorrect
The key must be a string literal with quotes to correctly observe LiveData.
4fill in blank
hardFill both blanks to create a SavedStateHandle with initial values.
Android Kotlin
val initialState = mapOf([1] to "Alice", [2] to 25) initialState.forEach { (k, v) -> savedStateHandle.set(k, v) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys in the map.
Using variable names instead of string keys.
✗ Incorrect
Keys in maps and SavedStateHandle must be string literals with quotes.
5fill in blank
hardFill all three blanks to update and retrieve a value safely from SavedStateHandle.
Android Kotlin
savedStateHandle.set([1], 100) val score: Int = savedStateHandle.get<Int>([2]) ?: [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for set and get.
Not providing a default value causing null errors.
Using unquoted keys.
✗ Incorrect
Use the same quoted key for set and get, and provide a default value if null.