# 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)

*** ** * ** ***

Common/AllAndroid/JVM
- android

  ```
  class MutableLiveData<T> : LiveData<T!>
  ```

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.lifecycle.LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData)\<T!\> ||
|   | ↳ | [androidx.lifecycle.MutableLiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData) |

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

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

*** ** * ** ***

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

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

## Summary

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

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData#postValue(T)(value: T!)` Posts a task to a main thread to set the given value. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/MutableLiveData#setValue(T)(value: T!)` Sets the value. | android |

### Inherited functions

From [androidx.lifecycle.LiveData](https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData) `T?` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#getValue()()`

Returns the current value.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#hasActiveObservers()()`

Returns true if this LiveData has active observers.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#hasObservers()()`

Returns true if this LiveData has observers.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#isInitialized()()`

Returns whether an explicit value has been set on this LiveData.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#observe(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Observer<? super T>)(owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner, observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>)`

Adds the given observer to the observers list within the lifespan of the given owner.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#observeForever(androidx.lifecycle.Observer<? super T>)(observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>)`

Adds the given observer to the observers list.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#onActive()()`

Called when the number of active observers change from 0 to 1.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#onInactive()()`

Called when the number of active observers change from 1 to 0.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#removeObserver(androidx.lifecycle.Observer<? super T>)(observer: https://developer.android.com/reference/kotlin/androidx/lifecycle/Observer<T!>)`

Removes the given observer from the observers list.
android `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData#removeObservers(androidx.lifecycle.LifecycleOwner)(owner: https://developer.android.com/reference/kotlin/androidx/lifecycle/LifecycleOwner)`

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

## Public constructors

### MutableLiveData

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

```
MutableLiveData()
```

Creates a MutableLiveData with no value assigned to it.

### MutableLiveData

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

```
MutableLiveData(value: T!)
```

Creates a MutableLiveData initialized with the given `value`.

| Parameters |
|---|---|
| `value: T!` | initial value |

## Public functions

### postValue

android

```
fun postValue(value: T!): Unit
```

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

```kotlin
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 |
|---|---|
| `value: T!` | The new value |

### setValue

android

```
fun setValue(value: T!): Unit
```

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/kotlin/androidx/lifecycle/LiveData#postValue(T)`

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