Complete the code to save a string value in the instance state bundle.
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("KEY_NAME", [1])
}You save the variable name into the bundle with putString.
Complete the code to restore the saved string value from the instance state bundle.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState != null) {
name = savedInstanceState.[1]("KEY_NAME") ?: ""
}
}getInt which returns an integer, not a string.getSerializable which is for objects, not strings.Use getString to retrieve the saved string from the bundle.
Fix the error in saving an integer counter in the instance state bundle.
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.[1]("COUNTER", counter)
}putString for an integer value causes a type error.putSerializable unnecessarily complicates saving simple types.Use putInt to save an integer value in the bundle.
Fill both blanks to restore an integer counter and provide a default value if missing.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
counter = savedInstanceState?.[1]("COUNTER") ?: [2]
}getString to get an integer causes errors.Use getInt to get the integer and 0 as the default value if none is saved.
Fill all three blanks to save and restore a boolean flag in the instance state.
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]
}putInt or putString for boolean values.true as default without reason.Use putBoolean to save, getBoolean to restore, and false as the default flag value.
