public class

MutableLiveData<T>

extends LiveData<java.lang.Object>

 java.lang.Object

androidx.lifecycle.LiveData<java.lang.Object>

↳androidx.lifecycle.MutableLiveData<T>

Subclasses:

MediatorLiveData<T>

Gradle dependencies

compile group: 'androidx.lifecycle', name: 'lifecycle-livedata-core', version: '2.5.0-rc01'

  • groupId: androidx.lifecycle
  • artifactId: lifecycle-livedata-core
  • version: 2.5.0-rc01

Artifact androidx.lifecycle:lifecycle-livedata-core:2.5.0-rc01 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.lifecycle:lifecycle-livedata-core android.arch.lifecycle:livedata-core

Androidx class mapping:

androidx.lifecycle.MutableLiveData android.arch.lifecycle.MutableLiveData

Overview

LiveData which publicly exposes MutableLiveData and MutableLiveData method.

Summary

Constructors
publicMutableLiveData()

Creates a MutableLiveData with no value assigned to it.

publicMutableLiveData(java.lang.Object value)

Creates a MutableLiveData initialized with the given value.

Methods
protected voidpostValue(java.lang.Object value)

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

protected voidsetValue(java.lang.Object value)

Sets the value.

from LiveData<T>getValue, hasActiveObservers, hasObservers, observe, observeForever, onActive, onInactive, removeObserver, removeObservers
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public MutableLiveData(java.lang.Object value)

Creates a MutableLiveData initialized with the given value.

Parameters:

value: initial value

public MutableLiveData()

Creates a MutableLiveData with no value assigned to it.

Methods

protected void postValue(java.lang.Object 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: The new value

protected void setValue(java.lang.Object 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 LiveData.postValue(T)

Parameters:

value: The new value

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.lifecycle;

/**
 * {@link LiveData} which publicly exposes {@link #setValue(T)} and {@link #postValue(T)} method.
 *
 * @param <T> The type of data hold by this instance
 */
@SuppressWarnings("WeakerAccess")
public class MutableLiveData<T> extends LiveData<T> {

    /**
     * Creates a MutableLiveData initialized with the given {@code value}.
     *
     * @param value initial value
     */
    public MutableLiveData(T value) {
        super(value);
    }

    /**
     * Creates a MutableLiveData with no value assigned to it.
     */
    public MutableLiveData() {
        super();
    }

    @Override
    public void postValue(T value) {
        super.postValue(value);
    }

    @Override
    public void setValue(T value) {
        super.setValue(value);
    }
}