Challenge - 5 Problems
Instance State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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() } } }
Attempts:
2 left
💡 Hint
Think about what happens to Activity variables when the screen rotates without saving state.
✗ Incorrect
By default, when the screen rotates, the Activity is destroyed and recreated. Without saving the TextView content in onSaveInstanceState and restoring it, the TextView resets to its initial empty state.
❓ lifecycle
intermediate1: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)?
Attempts:
2 left
💡 Hint
This method receives a Bundle to store key-value pairs for restoration.
✗ Incorrect
The onSaveInstanceState method is called before the Activity is destroyed to save transient UI state in a Bundle. This Bundle is passed back to onCreate or onRestoreInstanceState to restore the state.
📝 Syntax
advanced1: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
}Attempts:
2 left
💡 Hint
Use the Bundle method designed for strings.
✗ Incorrect
The Bundle class has a method putString(key: String, value: String?) to save strings. Other options are invalid or do not exist.
🔧 Debug
advanced2: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())
}Attempts:
2 left
💡 Hint
Check variable scope and where textView is declared.
✗ Incorrect
The textView variable used in onSaveInstanceState is undefined because it is declared only inside onCreate. This causes a runtime error or no value saved, so restoration fails.
🧠 Conceptual
expert1: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.
Attempts:
2 left
💡 Hint
Think about what happens when the screen rotates or the app is temporarily stopped.
✗ Incorrect
Saving instance state helps keep temporary UI data intact during configuration changes like screen rotation, so users do not lose their input or progress.