初始值为 MutableLiveData

如何使用初始值初始化 MutableLiveData? 我要找的是这样的东西:

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

78277 次浏览

Also this way..

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

UPDATE: Nowadays I would use MutableStateFlow instead of MutableLiveData with initial value.

https://developer.android.com/kotlin/flow/stateflow-and-sharedflow


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

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!")

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.

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

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.

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

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") }