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