Challenge - 5 Problems
LiveData Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
LiveData Observer Update Behavior
Given this LiveData and observer setup in an Android app, what will be the output on the UI after calling
liveData.value = "Hello"?Android Kotlin
val liveData = MutableLiveData<String>()
liveData.observe(this) { value ->
textView.text = value
}
liveData.value = "Hello"Attempts:
2 left
💡 Hint
LiveData notifies observers immediately when the value changes.
✗ Incorrect
When you set liveData.value, all active observers receive the new value right away, updating the UI.
❓ lifecycle
intermediate2:00remaining
LiveData and Lifecycle Owner
What happens if you observe LiveData with a lifecycle owner that is already in the DESTROYED state?
Android Kotlin
liveData.observe(lifecycleOwner) { value ->
// update UI
}Attempts:
2 left
💡 Hint
LiveData respects the lifecycle state of its observers.
✗ Incorrect
Observers tied to a destroyed lifecycle owner do not receive updates to avoid memory leaks.
📝 Syntax
advanced2:00remaining
Correct LiveData Declaration
Which of the following Kotlin code snippets correctly declares a MutableLiveData of integers and sets its initial value to 10?
Attempts:
2 left
💡 Hint
MutableLiveData initial value is set via the value property, not constructor.
✗ Incorrect
MutableLiveData constructor does not take initial value; set it by assigning to value property.
🔧 Debug
advanced2:00remaining
LiveData Observer Not Updating UI
Why does this LiveData observer not update the TextView when the value changes?
Android Kotlin
val liveData = MutableLiveData<String>()
liveData.observe(this) { value ->
textView.text = value
}
// Later in code
liveData.postValue("Update")Attempts:
2 left
💡 Hint
postValue schedules update on main thread asynchronously.
✗ Incorrect
postValue posts update to main thread asynchronously; if UI update code runs immediately after postValue, UI may not reflect change yet.
🧠 Conceptual
expert3:00remaining
LiveData Behavior on Configuration Change
After a device rotation (configuration change), what happens to the LiveData observers and their data in a typical Android app using ViewModel and LiveData?
Attempts:
2 left
💡 Hint
ViewModel survives configuration changes to keep data.
✗ Incorrect
ViewModel instances survive configuration changes; LiveData inside them keeps data; new observers get last emitted value immediately.