Search code examples
androidhandlerandroid-livedatamutablelivedata

LiveData not updating after changed using Handler PostDelayed?


Here's my ViewModel

class MainViewModel(repository: MainActivityRepo) : ViewModel() {

val isLoading: MutableLiveData<Boolean> = MutableLiveData()

init {
    isLoading.value = false
    android.os.Handler().postDelayed({
        isLoading.value = true
        Timber.d("isCalled")
    }, 5000L)
     }
}

I debugged and checked and the log is working perfectly.

The first value of boolean is set correctly, while the second is not


Solution

  • On background thread, you can use post value instead of set value which will solve your problem!