added in version 1.0.0
belongs to Maven artifact android.arch.lifecycle:livedata-core:1.1.1

MutableLiveData

public class MutableLiveData
extends LiveData<T>

java.lang.Object
   ↳ android.arch.lifecycle.LiveData<T>
     ↳ android.arch.lifecycle.MutableLiveData<T>
MediatorLiveData<T> LiveData subclass which may observe other LiveData objects and react on OnChanged events from them. 


LiveData which publicly exposes setValue(T) and postValue(T) method.

Summary

Public constructors

MutableLiveData()

Public methods

void postValue(T value)

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

void setValue(T value)

Sets the value.

Inherited methods

T getValue()

Returns the current value.

boolean hasActiveObservers()

Returns true if this LiveData has active observers.

boolean hasObservers()

Returns true if this LiveData has observers.

void observe(LifecycleOwner owner, Observer<T> observer)

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

void observeForever(Observer<T> observer)

Adds the given observer to the observers list.

void onActive()

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

void onInactive()

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

void postValue(T value)

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

void removeObserver(Observer<T> observer)

Removes the given observer from the observers list.

void removeObservers(LifecycleOwner owner)

Removes all observers that are tied to the given LifecycleOwner.

void setValue(T value)

Sets the value.

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

MutableLiveData

added in version 1.0.0
MutableLiveData ()

Public methods

postValue

added in version 1.0.0
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
value T: The new value

setValue

added in version 1.0.0
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 postValue(Object)

Parameters
value T: The new value