Bird
Raised Fist0
Android Kotlinmobile~20 mins

Saving instance state in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Instance State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What happens to the TextView content after screen rotation?
Consider this Android Kotlin code snippet inside an Activity. The user types text into an EditText and clicks a button to copy it to a TextView. What will the TextView show after rotating the device screen?
Android Kotlin
class MainActivity : AppCompatActivity() {
  private lateinit var editText: EditText
  private lateinit var textView: TextView

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    editText = findViewById(R.id.editText)
    textView = findViewById(R.id.textView)

    findViewById<Button>(R.id.button).setOnClickListener {
      textView.text = editText.text.toString()
    }
  }
}
AThe EditText content is lost but TextView keeps its text.
BThe TextView keeps showing the copied text after rotation.
CThe app crashes on rotation due to missing state handling.
DThe TextView becomes empty after rotation.
Attempts:
2 left
💡 Hint
Think about what happens to Activity variables when the screen rotates without saving state.
❓ lifecycle
intermediate
1:30remaining
Which method is used to save UI state before Activity destruction?
In Android Kotlin, which lifecycle method should you override to save UI state before the Activity is destroyed (e.g., during rotation)?
AonSaveInstanceState(bundle: Bundle)
BonStop()
ConPause()
DonDestroy()
Attempts:
2 left
💡 Hint
This method receives a Bundle to store key-value pairs for restoration.
📝 Syntax
advanced
1:30remaining
What is the correct way to save a string in onSaveInstanceState?
Choose the correct Kotlin code snippet to save a string variable named 'userInput' into the Bundle in onSaveInstanceState.
Android Kotlin
override fun onSaveInstanceState(outState: Bundle) {
  super.onSaveInstanceState(outState)
  // Save userInput here
}
AoutState.put("input", userInput)
BoutState.putString("input", userInput)
CoutState["input"] = userInput
DoutState.saveString("input", userInput)
Attempts:
2 left
💡 Hint
Use the Bundle method designed for strings.
🔧 Debug
advanced
2:00remaining
Why does the restored TextView remain empty after rotation?
This code tries to restore a saved string to a TextView in onCreate but the TextView stays empty after rotation. What is the likely cause?
Android Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)

  val textView = findViewById<TextView>(R.id.textView)
  val savedText = savedInstanceState?.getString("text")
  textView.text = savedText ?: ""
}

override fun onSaveInstanceState(outState: Bundle) {
  super.onSaveInstanceState(outState)
  outState.putString("text", textView.text.toString())
}
AtextView is not declared as a class property, so onSaveInstanceState cannot access it.
BThe savedInstanceState Bundle is always null in onCreate.
ConSaveInstanceState is not called during rotation.
DThe key "text" used in getString and putString do not match.
Attempts:
2 left
💡 Hint
Check variable scope and where textView is declared.
🧠 Conceptual
expert
1:30remaining
Why is saving instance state important in Android apps?
Select the best explanation why saving instance state is crucial for a good user experience in Android apps.
AIt allows the app to run background tasks without interruption.
BIt improves app startup speed by caching data permanently on disk.
CIt preserves UI data during configuration changes like rotation, preventing data loss and user frustration.
DIt automatically backs up user data to the cloud.
Attempts:
2 left
💡 Hint
Think about what happens when the screen rotates or the app is temporarily stopped.

Practice

(1/5)
1. What is the main purpose of onSaveInstanceState in Android Kotlin apps?
easy
A. To initialize UI components when the app starts
B. To permanently store data in a database
C. To save UI data temporarily during events like screen rotation
D. To handle user input events like button clicks

Solution

  1. Step 1: Understand the role of onSaveInstanceState

    This method is called before an activity might be destroyed, such as during screen rotation, to save temporary UI data.
  2. Step 2: Differentiate from other data storage methods

    Permanent storage like databases is not handled here; onSaveInstanceState is for short-term state saving only.
  3. Final Answer:

    To save UI data temporarily during events like screen rotation -> Option C
  4. Quick Check:

    onSaveInstanceState saves temporary UI state [OK]
Hint: Remember: onSaveInstanceState saves temporary UI data [OK]
Common Mistakes:
  • Confusing onSaveInstanceState with permanent storage
  • Thinking it initializes UI components
  • Assuming it handles user input events
2. Which of the following is the correct way to save a string value "username" in onSaveInstanceState?
easy
A. bundle.putString("username", username)
B. outState.putString("username", username)
C. savedInstanceState.putString("username", username)
D. state.putString("username", username)

Solution

  1. Step 1: Identify the correct parameter name in onSaveInstanceState

    The Bundle parameter is named outState in onSaveInstanceState method.
  2. Step 2: Use the correct method to save a string

    Use outState.putString(key, value) to save the string value.
  3. Final Answer:

    outState.putString("username", username) -> Option B
  4. Quick Check:

    Use outState to save data in onSaveInstanceState [OK]
Hint: Use outState.putString() inside onSaveInstanceState [OK]
Common Mistakes:
  • Using savedInstanceState inside onSaveInstanceState
  • Using undefined variable names like bundle or state
  • Confusing parameter names in lifecycle methods
3. Given this code snippet inside an Activity:
override fun onSaveInstanceState(outState: Bundle) {
  super.onSaveInstanceState(outState)
  outState.putInt("score", 10)
}

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  val score = savedInstanceState?.getInt("score") ?: 0
  println(score)
}

What will be printed after a screen rotation?
medium
A. Crash due to NullPointerException
B. 0
C. null
D. 10

Solution

  1. Step 1: Understand saving the score in onSaveInstanceState

    The score 10 is saved in the outState bundle before the activity is destroyed.
  2. Step 2: Restore score in onCreate after rotation

    savedInstanceState contains the saved bundle, so getInt("score") returns 10, not null or 0.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Saved state restores score = 10 after rotation [OK]
Hint: savedInstanceState?.getInt() returns saved value or default [OK]
Common Mistakes:
  • Assuming savedInstanceState is always null
  • Forgetting to call super.onSaveInstanceState
  • Expecting 0 instead of restored value
4. What is wrong with this code snippet for saving instance state?
override fun onSaveInstanceState(outState: Bundle) {
  outState.putInt("count", count)
}

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  val count = savedInstanceState?.getInt("count") ?: 0
}
medium
A. Missing call to super.onSaveInstanceState in onSaveInstanceState
B. Using wrong key name "count"
C. Not initializing count variable before saving
D. Not checking savedInstanceState for null in onCreate

Solution

  1. Step 1: Check onSaveInstanceState method

    It lacks a call to super.onSaveInstanceState(outState), which is required to save default state.
  2. Step 2: Verify other parts

    Key name "count" is consistent, count variable assumed initialized, and null check is done in onCreate.
  3. Final Answer:

    Missing call to super.onSaveInstanceState in onSaveInstanceState -> Option A
  4. Quick Check:

    Always call super.onSaveInstanceState [OK]
Hint: Always call super.onSaveInstanceState in override [OK]
Common Mistakes:
  • Forgetting super call in onSaveInstanceState
  • Using inconsistent keys for saving/restoring
  • Ignoring null checks on savedInstanceState
5. You want to save a checkbox state and a user input text during screen rotation. Which approach correctly saves and restores both in an Activity?
hard
A. Save checkbox state and text in onSaveInstanceState using outState.putBoolean and outState.putString, then restore in onCreate using savedInstanceState?.getBoolean and getString
B. Save checkbox state only in onSaveInstanceState, restore text from a global variable
C. Save text in onPause and checkbox state in onStop, restore both in onResume
D. Use SharedPreferences to save checkbox and text, restore in onCreate

Solution

  1. Step 1: Use onSaveInstanceState for temporary UI data

    Checkbox state (boolean) and text (string) should be saved in outState bundle in onSaveInstanceState.
  2. Step 2: Restore saved data in onCreate

    Use savedInstanceState?.getBoolean and getString to restore checkbox and text after rotation.
  3. Final Answer:

    Save checkbox state and text in onSaveInstanceState using outState.putBoolean and outState.putString, then restore in onCreate using savedInstanceState?.getBoolean and getString -> Option A
  4. Quick Check:

    Use onSaveInstanceState and savedInstanceState for UI state [OK]
Hint: Save UI data in onSaveInstanceState, restore in onCreate [OK]
Common Mistakes:
  • Using global variables that reset on rotation
  • Saving data in lifecycle methods not meant for state
  • Using persistent storage for temporary UI state
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard