4

I am using MutableLiveData to store a choice user has made. The value is set from another activity. SO onResume i am calling

 myMutableLivedata.value = newvale

But this does not update the UI unless i call a invalidateall().

Is this expected behaviour of MutableLiveData

png
  • 4,368
  • 7
  • 69
  • 118
  • Where are you setting the value of your `LiveData`, and where are you `Observering` it? – advice Jul 30 '18 at 20:18
  • I am setting it in the on Resume again to reflect the change. I do not observe it explicitly. I have not used mutablelivedata much. How do i observe for the changes. – png Jul 30 '18 at 20:20

1 Answers1

8

For LiveData, you need to Observe the changes.

First, create your MediatorLiveData.

val liveData = MediatorLiveData<MyItem>()

Next, within your Activity/Fragment/Etc, you need to call .observe. When the Observer is fired, it will have the updated data.

liveData.observe(this, Observer { 
    // Update the UI when the data has changed
})

And lastly, somewhere else in code, you can update the value of your LiveData, and the Observer will be notified.

// Somewhere else
liveData.value = MyItem("new value")
advice
  • 5,778
  • 10
  • 33
  • 60