0
0
Android Kotlinmobile~10 mins

SavedStateHandle in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"userName"
BuserName
C"user_name"
Duser_name
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the key string.
Using a variable name without quotes.
2fill in blank
medium

Complete the code to save a value into SavedStateHandle.

Android Kotlin
savedStateHandle.set([1], "John")
Drag options to blanks, or click blank then click option'
Auser_name
BuserName
C"user_name"
D"userName"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys when calling set().
Confusing variable names with string keys.
3fill in blank
hard

Fix 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'
Auser_name
B"userName"
CuserName
D"user_name"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys causing compile errors.
Mixing variable names with string keys.
4fill in blank
hard

Fill 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'
A"name"
B"age"
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys in the map.
Using variable names instead of string keys.
5fill in blank
hard

Fill 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'
A"score"
C0
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for set and get.
Not providing a default value causing null errors.
Using unquoted keys.