I am following the google android project "guess it" through udacity where they introduce livedata and mutablelivedata. We've got to the point where we're creating a livedata equivalent to a mutablelivedata object and creating a get() backingproperty for the livedata to the mutablelivedata instance. We make all changes to the data in the viewModel using the mutablelivedata instance. Our UI Fragment sets observers on the viewModel's livedata objects, NOT the mutablelivedata objects.
Dispite the observer being on the livedata instance and not the mutablelivedata instance, when the mutablelivedata object is updated, the livedata observer code is being triggered. I love that it works like this but I don't exactly understand how it works. Can anyone explain this to me?
In ViewModel
val _word = MutableLiveData<String>()
val word : LiveData<String>
get() = _word
Edit in ViewModel
private fun nextWord() {
//Select and remove a word from the list
if (wordList.isEmpty()) {
//gameFinished()
} else {
_word.value = wordList.removeAt(0)
}
}
In UI Fragment
viewModel.word.observe(this, Observer{newWord ->
binding.wordText.text = newWord
})