0
0
Android Kotlinmobile~10 mins

Saving instance state 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 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'
AgetName()
Bname
Cthis.name
DsavedName
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method call instead of the variable.
Using a variable name that does not exist.
2fill in blank
medium

Complete 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'
AgetString
BgetCharSequence
CgetInt
DgetSerializable
Attempts:
3 left
💡 Hint
Common Mistakes
Using getInt which returns an integer, not a string.
Using getSerializable which is for objects, not strings.
3fill in blank
hard

Fix 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'
AputString
BputBoolean
CputSerializable
DputInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using putString for an integer value causes a type error.
Using putSerializable unnecessarily complicates saving simple types.
4fill in blank
hard

Fill 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'
AgetInt
B0
C1
DgetString
Attempts:
3 left
💡 Hint
Common Mistakes
Using getString to get an integer causes errors.
Using a default value other than zero without reason.
5fill in blank
hard

Fill 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'
AputBoolean
BgetBoolean
Cfalse
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using putInt or putString for boolean values.
Using true as default without reason.