MediatorLiveData
open class MediatorLiveData<T : Any!> : MutableLiveData<T>
kotlin.Any | |||
↳ | androidx.lifecycle.LiveData<T> | ||
↳ | androidx.lifecycle.MutableLiveData<T> | ||
↳ | 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 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
.
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)); </integer> </integer> </integer>
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); } } }); </integer>
Summary
Public constructors | |
---|---|
<init>()
|
Public methods | |
---|---|
open Unit |
Starts to listen the given |
open Unit |
removeSource(@NonNull toRemote: LiveData<S>) Stops to listen the given |
Protected methods | |
---|---|
open Unit |
onActive() |
open Unit |
Inherited functions | |
---|---|
Public constructors
<init>
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 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
.
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)); </integer> </integer> </integer>
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); } } }); </integer>
Public methods
addSource
@MainThread open fun <S : Any!> addSource(
@NonNull source: LiveData<S>,
@NonNull onChanged: Observer<in S>
): Unit
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<S>: the LiveData to listen to |
onChanged |
Observer<in S>: The observer that will receive the events |
<S> |
The type of data hold by source LiveData |
removeSource
@MainThread open fun <S : Any!> removeSource(@NonNull toRemote: LiveData<S>): Unit
Stops to listen the given LiveData
.
Parameters | |
---|---|
toRemote |
LiveData<S>: LiveData to stop to listen |
<S> |
the type of data hold by source LiveData |
Protected methods
onActive
@CallSuper protected open fun onActive(): Unit
onInactive
@CallSuper protected open fun onInactive(): Unit