Saving instance state helps keep your app's data safe when the screen rotates or the app is temporarily stopped. It prevents losing what the user was doing.
Saving instance state in Android Kotlin
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("key", "value")
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
val value = savedInstanceState.getString("key")
}Use onSaveInstanceState to save data before the activity is destroyed.
Use onRestoreInstanceState or savedInstanceState in onCreate to restore data.
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("score", 42)
}override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
val score = savedInstanceState.getInt("score")
}override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean("isChecked", true)
}This app has a button that increases a number shown on screen. When you rotate the phone, the number stays the same because it is saved and restored using instance state.
import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private var counter = 0 private lateinit var counterText: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) counterText = findViewById(R.id.counterText) val button = findViewById<Button>(R.id.incrementButton) if (savedInstanceState != null) { counter = savedInstanceState.getInt("counter") } counterText.text = counter.toString() button.setOnClickListener { counter++ counterText.text = counter.toString() } } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putInt("counter", counter) } }
Always call super.onSaveInstanceState(outState) to let the system save default state.
Instance state is for small amounts of data like UI state, not for large files or databases.
Data saved in instance state is lost if the app is fully closed or the device restarts.
Saving instance state keeps UI data safe during screen rotations or temporary stops.
Use onSaveInstanceState to save data and onRestoreInstanceState or savedInstanceState in onCreate to restore it.
Good for saving small, temporary data like counters, text input, or checkbox states.
Practice
onSaveInstanceState in Android Kotlin apps?Solution
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.Step 2: Differentiate from other data storage methods
Permanent storage like databases is not handled here; onSaveInstanceState is for short-term state saving only.Final Answer:
To save UI data temporarily during events like screen rotation -> Option CQuick Check:
onSaveInstanceState saves temporary UI state [OK]
- Confusing onSaveInstanceState with permanent storage
- Thinking it initializes UI components
- Assuming it handles user input events
onSaveInstanceState?Solution
Step 1: Identify the correct parameter name in onSaveInstanceState
The Bundle parameter is named outState in onSaveInstanceState method.Step 2: Use the correct method to save a string
Use outState.putString(key, value) to save the string value.Final Answer:
outState.putString("username", username) -> Option BQuick Check:
Use outState to save data in onSaveInstanceState [OK]
- Using savedInstanceState inside onSaveInstanceState
- Using undefined variable names like bundle or state
- Confusing parameter names in lifecycle methods
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?
Solution
Step 1: Understand saving the score in onSaveInstanceState
The score 10 is saved in the outState bundle before the activity is destroyed.Step 2: Restore score in onCreate after rotation
savedInstanceState contains the saved bundle, so getInt("score") returns 10, not null or 0.Final Answer:
10 -> Option DQuick Check:
Saved state restores score = 10 after rotation [OK]
- Assuming savedInstanceState is always null
- Forgetting to call super.onSaveInstanceState
- Expecting 0 instead of restored value
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt("count", count)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val count = savedInstanceState?.getInt("count") ?: 0
}Solution
Step 1: Check onSaveInstanceState method
It lacks a call to super.onSaveInstanceState(outState), which is required to save default state.Step 2: Verify other parts
Key name "count" is consistent, count variable assumed initialized, and null check is done in onCreate.Final Answer:
Missing call to super.onSaveInstanceState in onSaveInstanceState -> Option AQuick Check:
Always call super.onSaveInstanceState [OK]
- Forgetting super call in onSaveInstanceState
- Using inconsistent keys for saving/restoring
- Ignoring null checks on savedInstanceState
Solution
Step 1: Use onSaveInstanceState for temporary UI data
Checkbox state (boolean) and text (string) should be saved in outState bundle in onSaveInstanceState.Step 2: Restore saved data in onCreate
Use savedInstanceState?.getBoolean and getString to restore checkbox and text after rotation.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 AQuick Check:
Use onSaveInstanceState and savedInstanceState for UI state [OK]
- Using global variables that reset on rotation
- Saving data in lifecycle methods not meant for state
- Using persistent storage for temporary UI state
Ready to prove it?
Pass the test and this pattern moves unknown
