MediatorLiveData
-
android
class MediatorLiveData<T> : MutableLiveData
| kotlin.Any | |||
| ↳ | androidx.lifecycle.LiveData | ||
| ↳ | androidx.lifecycle.MutableLiveData | ||
| ↳ | androidx.lifecycle.MediatorLiveData |
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 MediatorLiveData object: liveDataMerger. Then, liveData1 and
liveData2 will become sources for the liveDataMerger and every time onChanged callback is called for either of them, we set a new value in liveDataMerger.
LiveData<Integer> liveData1 = ...; LiveData<Integer> liveData2 = ...; MediatorLiveData<Integer> 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<Integer>() { private int count = 1; @Override public void onChanged(@Nullable Integer s) { count++; liveDataMerger.setValue(s); if (count > 10) { liveDataMerger.removeSource(liveData1); } } });
| Parameters | |
|---|---|
<T> |
The type of data hold by this instance |
Summary
Public constructors |
|
|---|---|
|
Creates a MediatorLiveData with no value assigned to it. |
android
|
MediatorLiveData(value: T!)Creates a MediatorLiveData initialized with the given |
android
|
Public functions |
||
|---|---|---|
Unit |
@MainThreadStarts to listen to the given |
android
|
Unit |
@MainThreadStops to listen the given |
android
|
Protected functions |
||
|---|---|---|
Unit |
Called when the number of active observers change from 0 to 1. |
android
|
Unit |
Called when the number of active observers change from 1 to 0. |
android
|
Inherited functions |
||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||
Public constructors
MediatorLiveData
MediatorLiveData(value: T!)
Creates a MediatorLiveData initialized with the given value.
| Parameters | |
|---|---|
value: T! |
initial value |
Public functions
addSource
@MainThread
fun <S> addSource(source: LiveData<S!>, onChanged: Observer<S!>): Unit
Starts to listen to 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.
removeSource
@MainThread
fun <S> removeSource(toRemote: LiveData<S!>): Unit
Stops to listen the given LiveData.
| Parameters | |
|---|---|
<S> |
the type of data hold by |
toRemote: LiveData<S!> |
|
Protected functions
onActive
@CallSuper
protected fun onActive(): Unit
Called when the number of active observers change from 0 to 1.
This callback can be used to know that this LiveData is being used thus should be kept up to date.
onInactive
@CallSuper
protected fun onInactive(): Unit
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.