SavedStateHandle is a class that helps you save and restore UI-related data in ViewModels during process death or configuration changes, like screen rotations.
Click to reveal answer
beginner
How do you get a SavedStateHandle instance inside a ViewModel?
You receive it as a constructor parameter when you create your ViewModel by extending ViewModel and using SavedStateViewModelFactory or by viewModels() with saved state support.
Click to reveal answer
beginner
How do you save a value using SavedStateHandle?
Use the set(key, value) method. For example, savedStateHandle.set("count", 5) saves the number 5 with the key "count".
Click to reveal answer
beginner
How do you retrieve a saved value from SavedStateHandle?
Use the get<T>(key) method. For example, savedStateHandle.get<Int>("count") returns the saved integer or null if not found.
Click to reveal answer
intermediate
Why is SavedStateHandle better than just using onSaveInstanceState in Activities?
SavedStateHandle works inside ViewModels, keeping UI data separate from UI controllers. It survives process death and configuration changes without cluttering Activity or Fragment code.
Click to reveal answer
What is the main purpose of SavedStateHandle in Android Kotlin?
ATo save and restore UI-related data in ViewModels
BTo handle network requests
CTo manage database connections
DTo create UI layouts
✗ Incorrect
SavedStateHandle is designed to save and restore UI-related data inside ViewModels.
How do you typically receive a SavedStateHandle in a ViewModel?
AAs a constructor parameter
BBy calling a static method
CBy accessing a global variable
DBy inflating a layout
✗ Incorrect
SavedStateHandle is passed as a constructor parameter to the ViewModel.
Which method saves a value in SavedStateHandle?
Aput(key, value)
Bset(key, value)
Csave(key, value)
Dstore(key, value)
✗ Incorrect
The correct method to save data is set(key, value).
What happens to data saved in SavedStateHandle when the app process is killed and restarted?
AData is lost
BData is sent to the server
CData is saved to a file
DData is restored automatically
✗ Incorrect
SavedStateHandle restores data automatically after process death.
Which Android component benefits most from using SavedStateHandle?
AService
BActivity
CViewModel
DBroadcastReceiver
✗ Incorrect
SavedStateHandle is designed to be used inside ViewModels.
Explain how SavedStateHandle helps preserve UI state in Android apps.
Think about how your app remembers data after screen rotation or app restart.
You got /4 concepts.
Describe the steps to use SavedStateHandle in a ViewModel to save a counter value.
Imagine you want to keep track of a number even if the app restarts.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of SavedStateHandle in Android Kotlin?
easy
A. To save and restore small UI-related data automatically during configuration changes
B. To manage database connections
C. To handle network requests
D. To create UI layouts dynamically
Solution
Step 1: Understand the role of SavedStateHandle
SavedStateHandle is designed to save small pieces of data like UI state during configuration changes such as screen rotations.
Step 2: Compare with other options
Options A, B, and D relate to network requests, database connections, and UI layout creation, which are not the purpose of SavedStateHandle.
Final Answer:
To save and restore small UI-related data automatically during configuration changes -> Option A
Quick Check:
SavedStateHandle saves UI state = C [OK]
Hint: Remember: SavedStateHandle saves UI data on config changes [OK]
Common Mistakes:
Confusing SavedStateHandle with database or network management
Thinking it manages large data or files
Assuming it creates UI layouts
2.
Which of the following is the correct way to store a value "username" in SavedStateHandle named savedStateHandle?
?
easy
A. savedStateHandle.put("username", "John")
B. savedStateHandle.set("username", "John")
C. savedStateHandle.save("username", "John")
D. savedStateHandle.store("username", "John")
Solution
Step 1: Recall SavedStateHandle API methods
The correct method to store data is set(key, value).
Step 2: Check method names in options
Only savedStateHandle.set("username", "John") uses set, others use incorrect method names.
Final Answer:
savedStateHandle.set("username", "John") -> Option B
Quick Check:
Use set() to store data = A [OK]
Hint: Use set() to save data in SavedStateHandle [OK]
Common Mistakes:
Using put(), save(), or store() which do not exist
Confusing with SharedPreferences methods
Forgetting to use quotes around keys
3.
Given the code below, what will be the output of println(savedStateHandle.get<String>("name"))?
val savedStateHandle = SavedStateHandle()
savedStateHandle.set("name", "Alice")
savedStateHandle.set("age", 30)
savedStateHandle.set("name", "Bob")
medium
A. null
B. Alice
C. Bob
D. 30
Solution
Step 1: Analyze the set operations
The key "name" is first set to "Alice", then overwritten with "Bob".
Step 2: Determine the value returned by get
Getting "name" returns the latest value, which is "Bob".
Final Answer:
Bob -> Option C
Quick Check:
Last set value returned = A [OK]
Hint: Last set value for a key is returned by get() [OK]
Common Mistakes:
Assuming first set value is returned
Confusing key names
Expecting numeric value for string key
4.
Identify the error in the following code snippet using SavedStateHandle:
val savedStateHandle = SavedStateHandle()
savedStateHandle.set("count", 10)
val count: String = savedStateHandle.get("count") ?: "0"
println(count)
medium
A. println() cannot print variables
B. Missing key in get() method
C. SavedStateHandle cannot store Int values
D. Type mismatch: trying to assign Int to String without conversion
Solution
Step 1: Check variable types
The value stored is Int (10), but retrieved as String without conversion.
Step 2: Understand Kotlin type safety
Assigning Int to String variable causes a type mismatch error.
Final Answer:
Type mismatch: trying to assign Int to String without conversion -> Option D
Quick Check:
Type mismatch error = D [OK]
Hint: Match types when getting values from SavedStateHandle [OK]
Common Mistakes:
Ignoring type mismatch errors
Assuming automatic type conversion
Thinking SavedStateHandle only stores strings
5.
You want to save a user's score in SavedStateHandle and restore it after a configuration change. Which approach correctly ensures the score is saved and retrieved safely?
class ScoreViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() {
fun saveScore(score: Int) {
// What to do here?
}
fun getScore(): Int {
// What to do here?
}
}
hard
A. saveScore: savedStateHandle.set("score", score)
getScore: savedStateHandle.get<Int>("score") ?: 0
B. saveScore: savedStateHandle.put("score", score)
getScore: savedStateHandle.get("score") as Int
C. saveScore: savedStateHandle.save("score", score)
getScore: savedStateHandle.get("score") ?: 0
D. saveScore: savedStateHandle.set("score", score.toString())
getScore: savedStateHandle.get<String>("score")?.toInt() ?: 0
Solution
Step 1: Use correct methods to save and get data
Use set to save and get<Int> to retrieve with a default fallback.
Step 2: Validate type safety and default value
saveScore: savedStateHandle.set("score", score)
getScore: savedStateHandle.get<Int>("score") ?: 0 uses correct method names and safely returns 0 if no score is saved.
Final Answer:
saveScore: savedStateHandle.set("score", score)
getScore: savedStateHandle.get<Int>("score") ?: 0 -> Option A
Quick Check:
Use set() and get<Int>() with default = B [OK]
Hint: Always use set() and get<Type>() with default fallback [OK]
Common Mistakes:
Using non-existent methods like put() or save()
Storing Int as String unnecessarily
Not providing default value on get()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.