0
0
Android Kotlinmobile~5 mins

Why ViewModel survives configuration changes in Android Kotlin - Quick Recap

Choose your learning style9 modes available
Recall & Review
beginner
What is a ViewModel in Android?
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
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
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
What is a configuration change in Android?
AScreen rotation or language change
BChanging app theme
CInstalling a new app
DUpdating Android OS
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
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.