0
0
Android Kotlinmobile~20 mins

Why ViewModel survives configuration changes in Android Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ViewModel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does ViewModel survive configuration changes?
In Android development, why does a ViewModel instance survive configuration changes like screen rotations?
ABecause ViewModel uses static variables that persist across configuration changes.
BBecause ViewModel saves its data to a local database automatically before configuration changes.
CBecause ViewModel is tied to the lifecycle of the Activity or Fragment and is retained by the system during configuration changes.
DBecause ViewModel is recreated every time but restores its state from savedInstanceState bundle.
Attempts:
2 left
💡 Hint
Think about how Android manages lifecycle and retains objects during configuration changes.
ui_behavior
intermediate
2:00remaining
What happens to UI data in ViewModel after rotation?
If you store user input in a ViewModel, what will happen to that data after the device is rotated?
AThe data is cleared and replaced with default values after rotation.
BThe data remains intact and accessible from the ViewModel after rotation.
CThe data is saved to a file automatically and reloaded after rotation.
DThe data is lost because ViewModel is destroyed and recreated on rotation.
Attempts:
2 left
💡 Hint
Remember what ViewModel is designed to do with data during lifecycle changes.
lifecycle
advanced
2:00remaining
When is a ViewModel destroyed?
Considering Android lifecycle, when does the system destroy a ViewModel instance?
AWhen the associated Activity or Fragment is finishing and not just being recreated due to configuration changes.
BImmediately after every configuration change like rotation.
CWhen the app goes into the background for more than 5 minutes.
DWhen the user presses the home button.
Attempts:
2 left
💡 Hint
Think about the difference between finishing an Activity and just rotating the device.
navigation
advanced
2:00remaining
How does ViewModel scope affect navigation between fragments?
If two fragments share a ViewModel scoped to their parent Activity, what happens to the ViewModel when navigating between these fragments?
AThe same ViewModel instance is shared and survives navigation between fragments.
BEach fragment gets a new ViewModel instance when navigated to.
CThe ViewModel is destroyed when leaving the first fragment and recreated for the second.
DThe ViewModel data is saved to disk and reloaded for each fragment.
Attempts:
2 left
💡 Hint
Consider the scope of the ViewModel and how it relates to the lifecycle owner.
🔧 Debug
expert
2:00remaining
Why does this ViewModel data reset after rotation?
You have this code snippet in your Activity: val viewModel = ViewModelProvider(this).get(MyViewModel::class.java) But after rotating the device, the data in viewModel resets. What is the most likely cause?
AViewModel does not survive rotation by design; you must manually save and restore data.
BYou forgot to save the ViewModel data in onSaveInstanceState.
CYou should use application context instead of Activity context to create ViewModelProvider.
DYou are creating a new ViewModelProvider with 'this' referring to the Activity, but the Activity is recreated, so the ViewModel instance is new.
Attempts:
2 left
💡 Hint
Check what 'this' refers to and how ViewModelProvider works with lifecycle owners.