# MediatorLiveData

added in[version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact android.arch.lifecycle:livedata:1.1.1  
Summary:[Ctors](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#pubctors)\|[Methods](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#inhmethods)  

# MediatorLiveData

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.lifecycle.MediatorLiveData](https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData)instead.

`
public


class
MediatorLiveData
`  
`


``


``

extends `[MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)`<T>


``


`

|---|---|---|----------------------------------------------|
| java.lang.Object                                      ||||
| ↳ | [android.arch.lifecycle.LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)\<T\> |||
|   | ↳ | [android.arch.lifecycle.MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)\<T\> ||
|   |   | ↳ | android.arch.lifecycle.MediatorLiveData\<T\> |

<br />

*** ** * ** ***

[LiveData](https://developer.android.com/reference/android/arch/lifecycle/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 liveData1 = ...;
 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);
          }
      }
 });
 
```

<br />

## Summary

|                                                      ### Public constructors                                                      ||
|--------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[MediatorLiveData](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#MediatorLiveData())`() ` |

|                                                                                                                                                                                                                                                 ### Public methods                                                                                                                                                                                                                                                  ||
|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` <S> void` | ` `[addSource](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#addSource(android.arch.lifecycle.LiveData<S>, android.arch.lifecycle.Observer<S>))`(`[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<S> source, `[Observer](https://developer.android.com/reference/android/arch/lifecycle/Observer)`<S> onChanged) ` Starts to listen the given`source`LiveData,`onChanged`observer will be called when`source`value was changed. |
| ` <S> void` | ` `[removeSource](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#removeSource(android.arch.lifecycle.LiveData<S>))`(`[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)`<S> toRemote) ` Stops to listen the given`LiveData`.                                                                                                                                                                                                         |

|                                                                                   ### Protected methods                                                                                    ||
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[onActive](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#onActive())`() ` Called when the number of active observers change to 1 from 0.     |
| ` void` | ` `[onInactive](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData#onInactive())`() ` Called when the number of active observers change from 1 to 0. |

| ### Inherited methods |
|-----------------------|---|
| From class` `[android.arch.lifecycle.MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)` ` |---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` void` | ` `[postValue](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData#postValue(T))`(T value) ` Posts a task to a main thread to set the given value. | | ` void` | ` `[setValue](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData#setValue(T))`(T value) ` Sets the value.                                         | ||
| From class` `[android.arch.lifecycle.LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)` ` |------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` T`       | ` `[getValue](https://developer.android.com/reference/android/arch/lifecycle/LiveData#getValue())`() ` Returns the current value.                                                                                                                                                                                                                                                                                                                                             | | ` boolean` | ` `[hasActiveObservers](https://developer.android.com/reference/android/arch/lifecycle/LiveData#hasActiveObservers())`() ` Returns true if this LiveData has active observers.                                                                                                                                                                                                                                                                                                | | ` boolean` | ` `[hasObservers](https://developer.android.com/reference/android/arch/lifecycle/LiveData#hasObservers())`() ` Returns true if this LiveData has observers.                                                                                                                                                                                                                                                                                                                   | | ` void`    | ` `[observe](https://developer.android.com/reference/android/arch/lifecycle/LiveData#observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer<T>))`(`[LifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner)` owner, `[Observer](https://developer.android.com/reference/android/arch/lifecycle/Observer)`<T> observer) ` Adds the given observer to the observers list within the lifespan of the given owner. | | ` void`    | ` `[observeForever](https://developer.android.com/reference/android/arch/lifecycle/LiveData#observeForever(android.arch.lifecycle.Observer<T>))`(`[Observer](https://developer.android.com/reference/android/arch/lifecycle/Observer)`<T> observer) ` Adds the given observer to the observers list.                                                                                                                                                                          | | ` void`    | ` `[onActive](https://developer.android.com/reference/android/arch/lifecycle/LiveData#onActive())`() ` Called when the number of active observers change to 1 from 0.                                                                                                                                                                                                                                                                                                         | | ` void`    | ` `[onInactive](https://developer.android.com/reference/android/arch/lifecycle/LiveData#onInactive())`() ` Called when the number of active observers change from 1 to 0.                                                                                                                                                                                                                                                                                                     | | ` void`    | ` `[postValue](https://developer.android.com/reference/android/arch/lifecycle/LiveData#postValue(T))`(T value) ` Posts a task to a main thread to set the given value.                                                                                                                                                                                                                                                                                                        | | ` void`    | ` `[removeObserver](https://developer.android.com/reference/android/arch/lifecycle/LiveData#removeObserver(android.arch.lifecycle.Observer<T>))`(`[Observer](https://developer.android.com/reference/android/arch/lifecycle/Observer)`<T> observer) ` Removes the given observer from the observers list.                                                                                                                                                                     | | ` void`    | ` `[removeObservers](https://developer.android.com/reference/android/arch/lifecycle/LiveData#removeObservers(android.arch.lifecycle.LifecycleOwner))`(`[LifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner)` owner) ` Removes all observers that are tied to the given[LifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner).                                                             | | ` void`    | ` `[setValue](https://developer.android.com/reference/android/arch/lifecycle/LiveData#setValue(T))`(T value) ` Sets the value.                                                                                                                                                                                                                                                                                                                                                | ||
| From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### MediatorLiveData

added in[version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
MediatorLiveData ()
```

<br />

<br />

## Public methods

### addSource

added in[version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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](https://developer.android.com/reference/java/lang/IllegalArgumentException)will be thrown.

<br />

|                                Parameters                                 ||
|-------------|--------------------------------------------------------------|
| `source`    | `LiveData`: the`LiveData`to listen to <br />                 |
| `onChanged` | `Observer`: The observer that will receive the events <br /> |

### removeSource

added in[version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void removeSource (LiveData<S> toRemote)
```

Stops to listen the given`LiveData`.

<br />

|                        Parameters                         ||
|------------|-----------------------------------------------|
| `toRemote` | `LiveData`:`LiveData`to stop to listen <br /> |

## Protected methods

### onActive

added in[version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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.

<br />

### onInactive

added in[version 1.0.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
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](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#STARTED)or[RESUMED](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#RESUMED)(like an Activity in the back stack).

You can check if there are observers via[hasObservers()](https://developer.android.com/reference/android/arch/lifecycle/LiveData#hasObservers()).

<br />

-

  ## Annotations

  - [OnLifecycleEvent](https://developer.android.com/reference/android/arch/lifecycle/OnLifecycleEvent)
-

  ## Interfaces

  - [DefaultLifecycleObserver](https://developer.android.com/reference/android/arch/lifecycle/DefaultLifecycleObserver)
  - [LifecycleObserver](https://developer.android.com/reference/android/arch/lifecycle/LifecycleObserver)
  - [LifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner)
  - [LifecycleRegistryOwner](https://developer.android.com/reference/android/arch/lifecycle/LifecycleRegistryOwner)
  - [Observer](https://developer.android.com/reference/android/arch/lifecycle/Observer)
  - [ViewModelProvider.Factory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.Factory)
  - [ViewModelStoreOwner](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStoreOwner)
-

  ## Classes

  - [AndroidViewModel](https://developer.android.com/reference/android/arch/lifecycle/AndroidViewModel)
  - [Lifecycle](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle)
  - [LifecycleRegistry](https://developer.android.com/reference/android/arch/lifecycle/LifecycleRegistry)
  - [LifecycleService](https://developer.android.com/reference/android/arch/lifecycle/LifecycleService)
  - [LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)
  - [LiveDataReactiveStreams](https://developer.android.com/reference/android/arch/lifecycle/LiveDataReactiveStreams)
  - [MediatorLiveData](https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData)
  - [MutableLiveData](https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData)
  - [ProcessLifecycleOwner](https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner)
  - [ServiceLifecycleDispatcher](https://developer.android.com/reference/android/arch/lifecycle/ServiceLifecycleDispatcher)
  - [Transformations](https://developer.android.com/reference/android/arch/lifecycle/Transformations)
  - [ViewModel](https://developer.android.com/reference/android/arch/lifecycle/ViewModel)
  - [ViewModelProvider](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider)
  - [ViewModelProvider.AndroidViewModelFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.AndroidViewModelFactory)
  - [ViewModelProvider.NewInstanceFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider.NewInstanceFactory)
  - [ViewModelProviders](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders)
  - [ViewModelProviders.DefaultFactory](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders.DefaultFactory)
  - [ViewModelStore](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStore)
  - [ViewModelStores](https://developer.android.com/reference/android/arch/lifecycle/ViewModelStores)
-

  ## Enums

  - [Lifecycle.Event](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.Event)
  - [Lifecycle.State](https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State)