O Google tem o compromisso de promover a igualdade racial para as comunidades negras. Saiba como.


open class MediatorLiveData<T : Any!> : MutableLiveData<T>
   ↳ 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.

  liveData1 = ...;
   liveData2 = ...; MediatorLiveData
    liveDataMerger = new MediatorLiveData&lt;&gt;(); liveDataMerger.addSource(liveData1, value -&gt; liveDataMerger.setValue(value)); liveDataMerger.addSource(liveData2, value -&gt; 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) {
           if (count &gt; 10) {


Public constructors

LiveData subclass which may observe other LiveData objects and react on OnChanged events from them.

Public methods
open Unit
addSource(@NonNull source: LiveData<S>, @NonNull onChanged: Observer<in S>)

Starts to listen the given source LiveData, onChanged observer will be called when source value was changed.

open Unit
removeSource(@NonNull toRemote: LiveData<S>)

Stops to listen the given LiveData.

Protected methods
open Unit

open Unit

Inherited functions