Skip to content Skip to sidebar Skip to footer

Observe Mutable Live Data Of A Data Class's Property Changes

In Android Kotlin Fundamentals, the code mentions of using a backing property to encapsulate the MutableLiveData in ViewModel so that only the ViewModel itself can change said muta

Solution 1:

A MutableLiveData only notifies its observers if the value is changed. Changes to the class (Course in your case) are not observed.

For best practice, consider using an immutable data class for Course and assigning a new one to the LiveData each time:

dataclassCourse(
    val name: String,
    val category: String,
    val progress: Int
)

val course = /* TODO: get current course */val newCourse = course.copy(progress = course.progress + 5)
_lastAccessedCourse.value = newCourse

Post a Comment for "Observe Mutable Live Data Of A Data Class's Property Changes"