Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a LiveData variable named data of type String.
Android Kotlin
val data: LiveData<[1]> = MutableLiveData() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong data type like Int when you want to hold text.
Forgetting to specify the type inside the angle brackets.
✗ Incorrect
LiveData holds data of a specific type. Here, we want it to hold strings, so we use LiveData.
2fill in blank
mediumComplete the code to observe the LiveData data inside an Activity and update a TextView when data changes.
Android Kotlin
data.observe(this, [1] { newValue -> textView.text = newValue }) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using observeForever inside an Activity which can cause memory leaks.
Passing a wrong method name like observeData.
✗ Incorrect
To observe LiveData changes, you pass an Observer object that reacts to updates.
3fill in blank
hardFix the error in this code that tries to update LiveData value inside a ViewModel.
Android Kotlin
private val _data = MutableLiveData<String>()
val data: LiveData<String> = _data
fun updateData(newValue: String) {
_data.[1] = newValue
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using postValue on the main thread when setValue or value assignment is preferred.
Trying to call a method named updateValue which does not exist.
✗ Incorrect
To update LiveData value on the main thread, assign the new value to the 'value' property.
4fill in blank
hardFill both blanks to create a LiveData that transforms another LiveData's data by converting it to uppercase.
Android Kotlin
val originalData: LiveData<String> = MutableLiveData() val upperData: LiveData<String> = Transformations.[1](originalData) { it.[2]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using switchMap instead of map when only transforming data.
Using the deprecated 'toUpperCase()' method.
✗ Incorrect
Use Transformations.map to create a LiveData that applies a function to the original data. The modern Kotlin method to convert to uppercase is 'uppercase()'.
5fill in blank
hardFill all three blanks to create a ViewModel with MutableLiveData, expose it as LiveData, and update its value safely.
Android Kotlin
class MyViewModel : ViewModel() { private val _count = MutableLiveData<Int>().apply { value = [1] } val count: LiveData<Int> = [2] fun increment() { val current = _count.value ?: 0 _count.[3] = current + 1 } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Exposing the MutableLiveData directly instead of as LiveData.
Using the wrong property or method to update the LiveData value.
✗ Incorrect
Initialize MutableLiveData with 0, expose it as LiveData by returning _count, and update the value property to increment.