What is the purpose of saving instance state in Android?
Saving instance state helps preserve UI data and user inputs when the app is temporarily stopped or rotated, so the user doesn't lose their progress.
Click to reveal answer
beginner
Which Android lifecycle method is used to save instance state?
The onSaveInstanceState() method is called to save data before the activity might be destroyed.
Click to reveal answer
beginner
How do you restore saved instance state in an Android activity?
You restore saved data in onCreate() or onRestoreInstanceState() by reading from the Bundle passed as a parameter.
Click to reveal answer
intermediate
What kind of data should you save in the instance state Bundle?
Save small, simple UI-related data like text inputs, scroll positions, or selected items. Avoid saving large or complex objects.
Click to reveal answer
beginner
True or False: The instance state Bundle is saved permanently on the device.
False. The instance state Bundle is temporary and only used to restore UI state during short-term interruptions like rotation or backgrounding.
Click to reveal answer
Which method is called to save UI state before an activity is destroyed?
AonPause()
BonSaveInstanceState()
ConDestroy()
DonStart()
✗ Incorrect
The onSaveInstanceState() method is specifically designed to save UI state before destruction.
Where do you typically restore saved instance state data?
AonCreate() or onRestoreInstanceState()
BonStop()
ConResume()
DonDestroy()
✗ Incorrect
Saved state is restored in onCreate() or onRestoreInstanceState() using the Bundle parameter.
What type of data is best to save in the instance state Bundle?
ADatabase connections
BLarge images or files
CNetwork sockets
DSmall UI data like text or scroll position
✗ Incorrect
Only small, simple UI data should be saved in the Bundle to keep it efficient.
True or False: onSaveInstanceState() is called when the user presses the back button.
AFalse
BDepends on Android version
COnly on some devices
DTrue
✗ Incorrect
onSaveInstanceState() is not called when the user presses back because the activity is finishing.
What happens if you don't save instance state during rotation?
AApp crashes immediately
BAndroid saves everything automatically
CUser input and UI state will be lost
DNothing changes
✗ Incorrect
Without saving state, the UI resets and user data is lost on rotation.
Explain how to save and restore UI state in an Android activity during screen rotation.
Think about which methods handle saving and restoring, and what kind of data to save.
You got /5 concepts.
Why is it important to save instance state in mobile apps? Give an example scenario.
Imagine what happens if the user types something and rotates the device.
You got /4 concepts.
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
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 C
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
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 B
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
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 D
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
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 A
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
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.