Challenge - 5 Problems
MVVM Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediateUnderstanding the role of ViewModel in MVVM
In the MVVM pattern, what is the primary responsibility of the ViewModel?
Attempts:
2 left
💡 Hint
Think about which component acts as a bridge between UI and data without knowing UI details.
✗ Incorrect
The ViewModel holds UI data and business logic, exposing data to the View without referencing it directly, enabling separation of concerns.
❓ ui_behavior
intermediateLiveData observation in MVVM
Given a ViewModel exposing LiveData called 'status', what happens in the View when the LiveData value changes?
Android Kotlin
viewModel.status.observe(viewLifecycleOwner) { newStatus ->
textView.text = newStatus
}Attempts:
2 left
💡 Hint
LiveData notifies observers when data changes.
✗ Incorrect
LiveData automatically notifies observers when its data changes, triggering UI updates like setting textView.text.
❓ lifecycle
advancedViewModel lifecycle and data retention
What happens to the ViewModel instance when the associated Activity is recreated due to a configuration change (e.g., device rotation)?
Attempts:
2 left
💡 Hint
ViewModel is designed to survive configuration changes.
✗ Incorrect
ViewModel instances survive configuration changes like rotations, so data is preserved and reused by the new Activity instance.
advanced
Sharing data between fragments using ViewModel
How can two fragments in the same Activity share data using MVVM pattern?
Attempts:
2 left
💡 Hint
Think about ViewModel scope and shared lifecycle owners.
✗ Incorrect
Using an Activity-scoped ViewModel allows multiple fragments to observe and update shared LiveData, enabling communication without tight coupling.
🔧 Debug
expertDiagnosing a memory leak caused by ViewModel misuse
A developer stores a reference to an Activity's Context inside a ViewModel. What is the likely consequence?
Attempts:
2 left
💡 Hint
Consider how long ViewModel lives compared to Activity and what holding Context means.
✗ Incorrect
ViewModel lives longer than Activity during configuration changes; holding Activity Context causes memory leaks by preventing garbage collection.
