# MutableLiveData

Artifact: [androidx.lifecycle:lifecycle-livedata-core](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/MutableLiveData.java+class:androidx.lifecycle.MutableLiveData) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

*** ** * ** ***


```
public class MutableLiveData<T> extends LiveData<T>
```

<br />

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

Known direct subclasses [MediatorLiveData](https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData)

|---|---|
| `https://developer.android.com/reference/androidx/lifecycle/MediatorLiveData` | `https://developer.android.com/reference/androidx/lifecycle/LiveData` subclass which may observe other `LiveData` objects and react on `OnChanged` events from them. |

*** ** * ** ***

`https://developer.android.com/reference/androidx/lifecycle/LiveData` which publicly exposes `https://developer.android.com/reference/androidx/lifecycle/MutableLiveData#setValue(T)` and `https://developer.android.com/reference/androidx/lifecycle/MutableLiveData#postValue(T)` method.

| Parameters |
|---|---|
| `<T>` | The type of data hold by this instance |

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/lifecycle/MutableLiveData#MutableLiveData()()` Creates a MutableLiveData with no value assigned to it. |
| `https://developer.android.com/reference/androidx/lifecycle/MutableLiveData#MutableLiveData(T)(T value)` Creates a MutableLiveData initialized with the given `value`. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/lifecycle/MutableLiveData#postValue(T)(T value)` Posts a task to a main thread to set the given value. |
| `void` | `https://developer.android.com/reference/androidx/lifecycle/MutableLiveData#setValue(T)(T value)` Sets the value. |

### Inherited methods

From [androidx.lifecycle.LiveData](https://developer.android.com/reference/androidx/lifecycle/LiveData) `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html T` `https://developer.android.com/reference/androidx/lifecycle/LiveData#getValue()()`

Returns the current value.
`boolean` `https://developer.android.com/reference/androidx/lifecycle/LiveData#hasActiveObservers()()`

Returns true if this LiveData has active observers.
`boolean` `https://developer.android.com/reference/androidx/lifecycle/LiveData#hasObservers()()`

Returns true if this LiveData has observers.
`boolean` `https://developer.android.com/reference/androidx/lifecycle/LiveData#isInitialized()()`

Returns whether an explicit value has been set on this LiveData.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/lifecycle/LiveData#observe(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Observer<? super T>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/Observer<T> observer)`

Adds the given observer to the observers list within the lifespan of the given owner.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/lifecycle/LiveData#observeForever(androidx.lifecycle.Observer<? super T>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/Observer<T> observer)`

Adds the given observer to the observers list.
`void` `https://developer.android.com/reference/androidx/lifecycle/LiveData#onActive()()`

Called when the number of active observers change from 0 to 1.
`void` `https://developer.android.com/reference/androidx/lifecycle/LiveData#onInactive()()`

Called when the number of active observers change from 1 to 0.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/lifecycle/LiveData#removeObserver(androidx.lifecycle.Observer<? super T>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/Observer<T> observer)`

Removes the given observer from the observers list.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/lifecycle/LiveData#removeObservers(androidx.lifecycle.LifecycleOwner)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner owner)`

Removes all observers that are tied to the given `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner`.

## Public constructors

### MutableLiveData

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
public MutableLiveData()
```

Creates a MutableLiveData with no value assigned to it.

### MutableLiveData

Added in [2.1.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.1.0)

```
public MutableLiveData(T value)
```

Creates a MutableLiveData initialized with the given `value`.

| Parameters |
|---|---|
| `T value` | initial value |

## Public methods

### postValue

```
public void postValue(T value)
```

Posts a task to a main thread to set the given value. So if you have a following code executed in the main thread:

```
liveData.postValue("a");
liveData.setValue("b");
```
The value "b" would be set at first and later the main thread would override it with the value "a".

If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

| Parameters |
|---|---|
| `T value` | The new value |

### setValue

```
public void setValue(T value)
```

Sets the value. If there are active observers, the value will be dispatched to them.

This method must be called from the main thread. If you need set a value from a background thread, you can use `https://developer.android.com/reference/androidx/lifecycle/LiveData#postValue(T)`

| Parameters |
|---|---|
| `T value` | The new value |