Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android : mutablelivedata change is not updating the UI

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

like image 290
png Avatar asked Oct 17 '25 22:10

png


1 Answers

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")
like image 177
Advice-Dog Avatar answered Oct 20 '25 13:10

Advice-Dog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!