Reading the LiveData documentation found here, I came across this section:
If a lifecycle becomes inactive, it receives the latest data upon becoming active again. For example, an activity that was in the background receives the latest data right after it returns to the foreground.
When I create a blank project to test this, I discover that latest data is not dispatched when coming from background.
Sample code from onCreate():
viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
button.setOnClickListener {
viewModel.buttonClicked()
}
viewModel.textLiveData.observe(this, Observer {
textview.text = it
Log.d("TEST", "new data = $it")
})
When going to background and coming back, should the latest data that the LiveData is holding be dispatched again to the observer?
UPDATE:
ViewModel code as requested:
class MyViewModel : ViewModel() {
val textLiveData = MutableLiveData<String>()
fun buttonClicked() {
textLiveData.value = "new text value"
}
}