belongs to Maven artifact android.arch.lifecycle:livedata:1.1.1
MediatorLiveData
public
class
MediatorLiveData
extends MutableLiveData<T>
java.lang.Object | |||
↳ | android.arch.lifecycle.LiveData<T> | ||
↳ | android.arch.lifecycle.MutableLiveData<T> | ||
↳ | android.arch.lifecycle.MediatorLiveData<T> |
LiveData
subclass which may observe other LiveData
objects and react on
OnChanged
events from them.
This class correctly propagates its active/inactive states down to source LiveData
objects.
Consider the following scenario: we have 2 instances of LiveData
, let's name them
liveData1
and liveData2
, and we want to merge their emissions in one object:
liveDataMerger
. Then, liveData1
and liveData2
will become sources for
the MediatorLiveData liveDataMerger
and every time onChanged
callback
is called for either of them, we set a new value in liveDataMerger
.
LiveDataliveData1 = ...; LiveData liveData2 = ...; MediatorLiveData liveDataMerger = new MediatorLiveData<>(); liveDataMerger.addSource(liveData1, value -> liveDataMerger.setValue(value)); liveDataMerger.addSource(liveData2, value -> liveDataMerger.setValue(value));
Let's consider that we only want 10 values emitted by liveData1
, to be
merged in the liveDataMerger
. Then, after 10 values, we can stop listening to liveData1
and remove it as a source.
liveDataMerger.addSource(liveData1, new Observer() { private int count = 1; @Override public void onChanged(@Nullable Integer s) { count++; liveDataMerger.setValue(s); if (count > 10) { liveDataMerger.removeSource(liveData1); } } });
Summary
Public constructors | |
---|---|
MediatorLiveData()
|
Public methods | |
---|---|
<S>
void
|
addSource(LiveData<S> source, Observer<S> onChanged)
Starts to listen the given |
<S>
void
|
removeSource(LiveData<S> toRemote)
Stops to listen the given |
Protected methods | |
---|---|
void
|
onActive()
Called when the number of active observers change to 1 from 0. |
void
|
onInactive()
Called when the number of active observers change from 1 to 0. |
Inherited methods | |
---|---|
Public constructors
Public methods
addSource
void addSource (LiveData<S> source, Observer<S> onChanged)
Starts to listen the given source
LiveData, onChanged
observer will be called
when source
value was changed.
onChanged
callback will be called only when this MediatorLiveData
is active.
If the given LiveData is already added as a source but with a different Observer,
IllegalArgumentException
will be thrown.
Parameters | |
---|---|
source |
LiveData : the LiveData to listen to |
onChanged |
Observer : The observer that will receive the events |
removeSource
void removeSource (LiveData<S> toRemote)
Stops to listen the given LiveData
.
Parameters | |
---|---|
toRemote |
LiveData : LiveData to stop to listen |
Protected methods
onActive
void onActive ()
Called when the number of active observers change to 1 from 0.
This callback can be used to know that this LiveData is being used thus should be kept up to date.
onInactive
void onInactive ()
Called when the number of active observers change from 1 to 0.
This does not mean that there are no observers left, there may still be observers but their
lifecycle states aren't STARTED
or RESUMED
(like an Activity in the back stack).
You can check if there are observers via hasObservers()
.
Annotations
Interfaces
Classes
- AndroidViewModel
- Lifecycle
- LifecycleRegistry
- LifecycleService
- LiveData
- LiveDataReactiveStreams
- MediatorLiveData
- MutableLiveData
- ProcessLifecycleOwner
- ServiceLifecycleDispatcher
- Transformations
- ViewModel
- ViewModelProvider
- ViewModelProvider.AndroidViewModelFactory
- ViewModelProvider.NewInstanceFactory
- ViewModelProviders
- ViewModelProviders.DefaultFactory
- ViewModelStore
- ViewModelStores
Enums