When to use MutableLiveData and LiveData

LiveData has no public method to modify its data. LiveData<User> getUser() { if (userMutableLiveData == null) { userMutableLiveData = new MutableLiveData<>(); } return userMutableLiveData } You can’t update its value like getUser().setValue(userObject) or getUser().postValue(userObject) So when you don’t want your data to be modified use LiveData If you want to modify your data later use … Read more

Paging library – Boundary callback for network + db with API taking page and size

The documentation has this to say on the issue: If you aren’t using an item-keyed network API, you may be using page-keyed, or page-indexed. If this is the case, the paging library doesn’t know about the page key or index used in the BoundaryCallback, so you need to track it yourself. You can do this … Read more

Use viewLifecycleOwner as the LifecycleOwner

Why I get this error? Lint is recommending that you use the lifecycle of the fragment’s views (viewLifecycleOwner) rather than the lifecycle of the fragment itself (this). Ian Lake and Jeremy Woods of Google go over the difference as part of this Android Developer Summit presentation, and Ibrahim Yilmaz covers the differences in this Medium … Read more

How to clear LiveData stored value?

Update There are actually a few ways to resolve this issue. They are summarized nicely in the article LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case). This is written by a fellow Googler who works with the Architecture Components team. TL;DR A more robust approach is to use an Event wrapper class, which … Read more

Live Data: Candidate resolution will be changed soon

It means that the extension in androidx is not needed anymore. Simply remove its import import androidx.lifecycle.observe. It will be actually deprecated in androidx. Read more reasoning there. EDIT: Please note the “issue” from Erik Hoogendoorn This change causes values from the observed LiveData object to be interpreted as nullable(since the converted lambda syntax is … Read more

MutableLiveData with initial value

MutableLiveData has been updated and now it has a constructor that accepts an initial value 🙂 From what I can see, the constructor is available starting from this version: implementation ‘androidx.lifecycle:lifecycle-extensions:2.1.0-alpha01′ It’s a shame they haven’t updated MediatorLiveData to reflect that, though. 2.1.0 is finally stable, so now you can actually see the new constructor … 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

What is difference between MediatorLiveData and MutableLiveData in MVVM

At first we need to know what is the relation between MutableLivedata and MediatorLivedata to understand the difference between them. java.lang.Object ↳ android.arch.lifecycle.LiveData<T> ↳ android.arch.lifecycle.MutableLiveData<T> ↳ android.arch.lifecycle.MediatorLiveData<T> Now it is clear that MediatorLiveData is a subclass of MutableLiveData therefore MediatorLiveData can access each and every property of MutableLiveData as well as LiveData. Question no. 1 … Read more