In the fragement I display a Date from a Calendar object. This date is got from the viewModel of the fragement and it's a live date :
private val _theDate = MutableLiveData<Calendar>()
val theDate: LiveData<Calendar>
get() = _theDate
In the viewModel also I have a function that add 1 day to _theDate
fun goToDayAfter() {
_theDate.value!!.add(Calendar.DATE, 1)
}
This function is called after clicking on a button of the same fragment, and it does not trigger the observer :
viewModel.theDate.observe(viewLifecycleOwner, androidx.lifecycle.Observer { newDate ->
displayDate(newDate)
})
For more details and after debugging, I believe that _theDate is well changed but the observer is not triggered, If I change to another fragement and come back the new _theDate is changed.
Also if I change the method goToDayAfter() to :
fun goToDayAfter() {
val tmp = _theDate.value!!
tmp.add(Calendar.DATE, 1)
_theDate.value = tmp
}
It works!
Why cahnging _theDate directly do not trigger the observer ? is it because it s an object and not a premitive ? Is there any better solution that to pass by a tmp variable ?