How to combine two live data one after the other?

You can use my helper method: val profile = MutableLiveData<ProfileData>() val user = MutableLiveData<CurrentUser>() val title = profile.combineWith(user) { profile, user -> “${profile.job} ${user.name}” } fun <T, K, R> LiveData<T>.combineWith( liveData: LiveData<K>, block: (T?, K?) -> R ): LiveData<R> { val result = MediatorLiveData<R>() result.addSource(this) { result.value = block(this.value, liveData.value) } result.addSource(liveData) { result.value = … Read more

Why LiveData observer is being triggered twice for a newly attached observer

I have introduced just one change in your code: noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class); instead of: noteViewModel = ViewModelProviders.of(getActivity()).get(NoteViewModel.class); in Fragment‘s onCreate(Bundle) methods. And now it works seamlessly. In your version you obtained a reference of NoteViewModel common to both Fragments (from Activity). ViewModel had Observer registered in previous Fragment, I think. Therefore LiveData kept reference to … Read more

LiveData remove Observer after first callback

There is a more convenient solution for Kotlin with extensions: fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) { observe(lifecycleOwner, object : Observer<T> { override fun onChanged(t: T?) { observer.onChanged(t) removeObserver(this) } }) } This extension permit us to do that: liveData.observeOnce(this, Observer<Password> { if (it != null) { // do something } }) So to answer … Read more

Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change

There is simple solution in Transformations method distinctUntilChanged.expose new data only if data was changed. In this case we get data only when it changes in source: LiveData<YourType> getData(){ return Transformations.distinctUntilChanged(LiveData<YourType> source)); } But for Event cases is better to use this: https://stackoverflow.com/a/55212795/9381524

Why there’s a separate MutableLiveData subclass of LiveData?

In LiveData – Android Developer Documentation, you can see that for LiveData, setValue() & postValue() methods are not public. Whereas, in MutableLiveData – Android Developer Documentation, you can see that, MutableLiveData extends LiveData internally and also the two magic methods of LiveData is publicly available in this and they are setValue() & postValue(). setValue(): set … Read more

Room – LiveData observer does not trigger when database is updated

I had a similar problem using Dagger 2 that was caused by having different instances of the Dao, one for updating/inserting data, and a different instance providing the LiveData for observing. Once I configured Dagger to manage a singleton instance of the Dao, then I could insert data in the background (in my case in … Read more

LiveData prevent receive the last value when start observing

I`m using this EventWraper class from Google Samples inside MutableLiveData /** * Used as a wrapper for data that is exposed via a LiveData that represents an event. */ public class Event<T> { private T mContent; private boolean hasBeenHandled = false; public Event( T content) { if (content == null) { throw new IllegalArgumentException(“null values … Read more