How to check if LiveData or MutableLiveData has been set a value?

Viewed 954

How can I check if a value has been set to a LiveData? I want to do this because if I haven't set a value yet, I will set a starting value, and from this starting value I will get the next one.

myMutable.getValue() !== null 

I want to do something like this.

1 Answers

Are you creating the LiveData?

If so, you can pass the initial value into the constructor upon creation.

Example:

LiveData<Integer> liveData = new LiveData<>(1);
//liveData.getValue() == 1

If not, you can check if the LiveData has a value set just as you specified but with the != operator. !== is not a valid java operator.

Related