I am implementing Android Architecture Components
. Imagine a case like the following where your Fragment
is observing a LiveData
to change its UI. User minimizes the app and the state is changed (in my case from the repository). So the Observer
from the Fragment
is not triggered with a change because the Fragment
is not visible. But then, when the user comes back to the app it doesn't trigger the new state. If the state is changed again (while the Fragment
is visible), the Observer
receives the change. Do you know any way to force an update when fragment is visible again?
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
vm = ViewModelProviders.of(this, viewModelFactory).get(MyViewModel::class.java)
vm.getStatus()?.observe(this, Observer<MyRepository.Status> { status ->
if (status != null) {
when (status) {
NONE -> setNoneUI()
LOADING -> setLoadingUI()
CONTENT -> setContentUI()
ERROR -> setErrorUI()
}
}
})
}
Actually your activity should receive the latest state from LiveData
when it is visible again, as explained in this video and described here:
Always up to date data: 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.
I just tested it in my own application, works as described. So there must be another error in your application: Do you set the LiveData
correctly from your repository class? Maybe a different thread / postValue
problem?