MutableLiveData

class MutableLiveData<T> : LiveData

Known direct subclasses
MediatorLiveData

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


LiveData which publicly exposes setValue and postValue method.

Parameters
<T>

The type of data hold by this instance

Summary

Public constructors

Creates a MutableLiveData with no value assigned to it.

MutableLiveData(value: T!)

Creates a MutableLiveData initialized with the given value.

Public functions

Unit
postValue(value: T!)

Posts a task to a main thread to set the given value.

Unit
setValue(value: T!)

Sets the value.

Inherited functions

From androidx.lifecycle.LiveData
T?

Returns the current value.

Boolean

Returns true if this LiveData has active observers.

Boolean

Returns true if this LiveData has observers.

Boolean

Returns whether an explicit value has been set on this LiveData.

Unit
@MainThread
observe(owner: LifecycleOwner, observer: Observer<Any!>)

Adds the given observer to the observers list within the lifespan of the given owner.

Unit

Adds the given observer to the observers list.

Unit

Called when the number of active observers change from 0 to 1.

Unit

Called when the number of active observers change from 1 to 0.

Unit

Removes the given observer from the observers list.

Unit

Removes all observers that are tied to the given LifecycleOwner.

Public constructors

MutableLiveData

Added in 2.0.0
MutableLiveData()

Creates a MutableLiveData with no value assigned to it.

MutableLiveData

Added in 2.1.0
MutableLiveData(value: T!)

Creates a MutableLiveData initialized with the given value.

Parameters
value: T!

initial value

Public functions

postValue

Added in 2.8.0-alpha01
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:

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

Added in 2.8.0-alpha01
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 postValue

Parameters
value: T!

The new value