MutableLiveData with initial value

AndroidKotlinAndroid Livedata

Android Problem Overview


How I can initialize MutableLiveData with initial value? I'm looking for something like:

val text = MutableLiveData<String>("initial value")

Android Solutions


Solution 1 - Android

MutableLiveData has been updated and now it has a constructor that accepts an initial value :)

From what I can see, the constructor is available starting from this version:

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha01'

It's a shame they haven't updated MediatorLiveData to reflect that, though.


2.1.0 is finally stable, so now you can actually see the new constructor in the documentation.

Solution 2 - Android

You can create a handy Extension Function that does the initialization for you.

fun <T : Any?> MutableLiveData<T>.default(initialValue: T) = apply { setValue(initialValue) }

val liveData = MutableLiveData<String>().default("Initial value!")

Solution 3 - Android

Finally, I realize that I can use

val text = MutableLiveData<String>().apply { postValue("initial value")}

(postValue is necessary when using from a background thread, otherwise when using from the main thread you can safely use value = "initial value")

Solution 4 - Android

Though this is a Kotlin question, I though it might be helpful if I put the Java version for this as well.

While using androidx dependency:

implementation "androidx.lifecycle:lifecycle-viewmodel:2.1.0"

Initialize the MutableLiveData using the constructor as follows.

new MutableLiveData<>("Initial value");

While using the android dependency:

implementation 'android.arch.lifecycle:viewmodel:1.1.1'

Initialize as follows.

MutableLiveData<String> text = MutableLiveData<String>();
text.setValue("Initial value");

The androidx dependency does have the constructor that takes a value for the initialization of your LiveData. However, if you are using the android dependency you will not have that option of doing the initialization using the constructor.

Solution 5 - Android

You can create an extension constructor (sugar trick) like this:

fun <T : Any?> MutableLiveData(defaultValue: T) = MutableLiveData<T>().apply { setValue(defaultValue) }

Using:

var defaultValue: MutableLiveData<String> = MutableLiveData("Default value")

Solution 6 - Android

Also this way..

    val data = MutableLiveData<String>()
    data.value = "data"
    val text = MutableLiveData<String>().apply {
        "apply"
    }
    Log.d("Data",text.value)

Solution 7 - Android

I have a better solution if you want some default value to be passed in your MutableLiveData<T> If you are using kotlin then there is a class called ObservableProperty<T> which can help you pass the default for your MutableLiveData.Here's my implementation.

val nameLiveData = MutableLiveData<String>()
var name: String by Delegates.observable("") { _, _, newValue ->
    nameLiveData.postValue(newValue)
}

In your Activity or Fragment observe this property.

viewModel.nameLiveData.observe(this, Observer {
        //Your logic goes here
    })

All you have to do change the value is do name = "Joe" and it will posted in your LiveData

Solution 8 - Android

I would do like this:

val text by lazy { MutableLiveData<String>("Initial text") }

This way, you are using lazy from kotlin and setting the default value. or even shorter like this:

val text by lazy { MutableLiveData<>("Initial text") }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrancisView Question on Stackoverflow
Solution 1 - AndroidFred PorciúnculaView Answer on Stackoverflow
Solution 2 - AndroidLionel BriandView Answer on Stackoverflow
Solution 3 - AndroidFrancisView Answer on Stackoverflow
Solution 4 - AndroidReaz MurshedView Answer on Stackoverflow
Solution 5 - AndroidMickael BelhassenView Answer on Stackoverflow
Solution 6 - AndroidMobile Team ADR-FlutterView Answer on Stackoverflow
Solution 7 - Androidpratham kesarkarView Answer on Stackoverflow
Solution 8 - AndroidAmin KeshavarzianView Answer on Stackoverflow