Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to save a string value in the instance state bundle.
Android Kotlin
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("KEY_NAME", [1])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method call instead of the variable.
Using a variable name that does not exist.
✗ Incorrect
You save the variable name into the bundle with putString.
2fill in blank
mediumComplete the code to restore the saved string value from the instance state bundle.
Android Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState != null) {
name = savedInstanceState.[1]("KEY_NAME") ?: ""
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
getInt which returns an integer, not a string.Using
getSerializable which is for objects, not strings.✗ Incorrect
Use getString to retrieve the saved string from the bundle.
3fill in blank
hardFix the error in saving an integer counter in the instance state bundle.
Android Kotlin
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.[1]("COUNTER", counter)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
putString for an integer value causes a type error.Using
putSerializable unnecessarily complicates saving simple types.✗ Incorrect
Use putInt to save an integer value in the bundle.
4fill in blank
hardFill both blanks to restore an integer counter and provide a default value if missing.
Android Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
counter = savedInstanceState?.[1]("COUNTER") ?: [2]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
getString to get an integer causes errors.Using a default value other than zero without reason.
✗ Incorrect
Use getInt to get the integer and 0 as the default value if none is saved.
5fill in blank
hardFill all three blanks to save and restore a boolean flag in the instance state.
Android Kotlin
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.[1]("FLAG", flag)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
flag = savedInstanceState?.[2]("FLAG") ?: [3]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
putInt or putString for boolean values.Using
true as default without reason.✗ Incorrect
Use putBoolean to save, getBoolean to restore, and false as the default flag value.