0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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.