A ViewModel is a class designed to store and manage UI-related data in a lifecycle conscious way. It helps data survive configuration changes like screen rotations.
Click to reveal answer
beginner
How does ViewModel survive configuration changes?
ViewModel objects are tied to the lifecycle of an activity or fragment but are retained when those components are recreated during configuration changes, so data is preserved.
Click to reveal answer
intermediate
What role does ViewModelStore play in ViewModel survival?
ViewModelStore holds ViewModel instances and is retained by the system during configuration changes, allowing ViewModels to persist across activity or fragment recreation.
Click to reveal answer
intermediate
Why is it better to use ViewModel instead of onSaveInstanceState for data persistence?
ViewModel can hold complex data and live longer during configuration changes without serialization, unlike onSaveInstanceState which only saves small amounts of serializable data.
Click to reveal answer
beginner
What happens to a ViewModel when the activity is finished or destroyed permanently?
When the activity or fragment is finished and not recreated, the ViewModel is cleared and its resources are released to avoid memory leaks.
Click to reveal answer
What allows a ViewModel to survive configuration changes?
AIt is stored in ViewModelStore which is retained by the system
BIt saves data to a file automatically
CIt uses onSaveInstanceState internally
DIt recreates data every time the activity restarts
✗ Incorrect
ViewModelStore keeps ViewModels alive during configuration changes, so data is preserved.
When does the ViewModel get cleared?
AWhen the user presses the back button once
BWhen the activity is recreated due to rotation
CWhen the app goes to background
DWhen the activity is finished permanently
✗ Incorrect
ViewModel is cleared only when the activity or fragment is finished and not recreated.
Which of these is NOT a benefit of using ViewModel?
ASurvives configuration changes
BAutomatically saves data to disk
CManages UI-related data lifecycle
DPrevents memory leaks by clearing data
✗ Incorrect
ViewModel does not save data to disk automatically; it holds data in memory.
What is a configuration change in Android?
AScreen rotation or language change
BChanging app theme
CInstalling a new app
DUpdating Android OS
✗ Incorrect
Configuration changes include screen rotations or language changes that recreate activities.
Why is ViewModel preferred over onSaveInstanceState for large data?
ABecause ViewModel saves data to a database
BBecause onSaveInstanceState is deprecated
CBecause onSaveInstanceState can only save small serializable data
DBecause ViewModel runs on a background thread
✗ Incorrect
onSaveInstanceState is limited to small serializable data; ViewModel can hold larger and complex data in memory.
Explain in your own words why ViewModel survives configuration changes in Android.
Think about what happens when the screen rotates and how ViewModel keeps data safe.
You got /3 concepts.
Describe the difference between ViewModel and onSaveInstanceState for handling data during configuration changes.
Consider how each method stores data and when it is restored.
You got /4 concepts.
Practice
(1/5)
1. Why does a ViewModel survive configuration changes like screen rotations in Android?
easy
A. Because it is tied to the lifecycle of the activity or fragment and not recreated on configuration changes
B. Because it saves data to a file on disk automatically
C. Because it uses static variables to hold data
D. Because it runs in a separate background thread
Solution
Step 1: Understand ViewModel lifecycle
A ViewModel is designed to be tied to the lifecycle of an activity or fragment but survives configuration changes like rotations.
Step 2: Why it survives configuration changes
When a configuration change happens, the activity is destroyed and recreated, but the ViewModel instance is retained and reused.
Final Answer:
Because it is tied to the lifecycle of the activity or fragment and not recreated on configuration changes -> Option A
Hint: Use ViewModelProvider(this).get() to get ViewModel [OK]
Common Mistakes:
Instantiating ViewModel directly with constructor
Using incorrect ViewModelProvider syntax
Confusing ViewModel class with instance
3. Given this code snippet in an Activity, what will be the output after a screen rotation?
class MyViewModel : ViewModel() {
var counter = 0
}
// In Activity
val model = ViewModelProvider(this).get(MyViewModel::class.java)
model.counter += 1
println(model.counter)
medium
A. 1 on first launch, then resets to 0 after rotation
B. 1 on first launch, then increments to 2 after rotation
C. Always prints 0
D. Throws an error on rotation
Solution
Step 1: Understand ViewModel data retention
The ViewModel instance survives configuration changes, so its data like counter is preserved.
Step 2: Trace counter increment
On first launch, counter is 0, incremented to 1 and printed. After rotation, the same ViewModel instance is used, counter is 1, incremented to 2 and printed.
Final Answer:
1 on first launch, then increments to 2 after rotation -> Option B
Quick Check:
ViewModel retains data = increments preserved [OK]
Hint: ViewModel keeps data, so counter increments across rotations [OK]
Common Mistakes:
Assuming ViewModel resets on rotation
Thinking counter always starts at 0
Expecting an error due to rotation
4. What is wrong with this code that tries to use ViewModel in an Activity?
class MyViewModel : ViewModel() {
var data = "Hello"
}
// In Activity
val model = MyViewModel()
println(model.data)
medium
A. The println statement is incorrect
B. The data variable must be private
C. ViewModel cannot have variables
D. ViewModel should not be instantiated directly; use ViewModelProvider instead
Solution
Step 1: Identify ViewModel instantiation error
ViewModel must be obtained via ViewModelProvider to survive configuration changes, not created with constructor.
Step 2: Explain consequence
Direct instantiation means a new ViewModel is created every time, losing data on rotation.
Final Answer:
ViewModel should not be instantiated directly; use ViewModelProvider instead -> Option D
Quick Check:
Use ViewModelProvider, not constructor [OK]
Hint: Never instantiate ViewModel with constructor directly [OK]
Common Mistakes:
Creating ViewModel with constructor
Thinking private variables fix lifecycle issues
Blaming println for errors
5. You want to keep user input data safe during screen rotations in your app. Which approach best uses ViewModel to achieve this?
hard
A. Store user input in a ViewModel property and update it on every input change
B. Save user input in SharedPreferences and reload after rotation
C. Use static variables in the Activity to hold user input
D. Recreate the ViewModel on each rotation and reset user input
Solution
Step 1: Identify best practice for preserving UI data
ViewModel is designed to hold UI-related data that survives configuration changes like rotations.
Step 2: Choose correct data handling method
Storing user input in a ViewModel property and updating it on input changes keeps data safe and avoids unnecessary disk operations.
Final Answer:
Store user input in a ViewModel property and update it on every input change -> Option A
Quick Check:
ViewModel holds UI data safely across rotations [OK]
Hint: Keep UI data in ViewModel properties updated live [OK]
Common Mistakes:
Using SharedPreferences for temporary UI data
Using static variables which can cause leaks
Recreating ViewModel loses data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.