0
0
Android Kotlinmobile~10 mins

LiveData basics in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AInt
BString
CBoolean
DFloat
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.
2fill in blank
medium

Complete 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'
Aobserve
BobserveForever
CobserveData
DObserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using observeForever inside an Activity which can cause memory leaks.
Passing a wrong method name like observeData.
3fill in blank
hard

Fix 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'
Avalue
BsetValue
CupdateValue
DpostValue
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.
4fill in blank
hard

Fill 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'
Amap
BswitchMap
CtoUpperCase
Duppercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using switchMap instead of map when only transforming data.
Using the deprecated 'toUpperCase()' method.
5fill in blank
hard

Fill 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'
A0
B_count
Cvalue
Dcount
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.