I use the livedata
in a Fragment that is in a viewpager. I observe a liveData in the onActivityCreated()
and display the data in a list. So far so good and I have no problem. On the other hand, when I put the application in background and return to any page of my viewpager, the Observer
is called again and I don't understand why! Please help me understand. I don't even go through the onResume()
of the fragment in question.
As you can see in the Image attached, the lifecycle of the Fragment is little different than that of activity, in fragment going to the background result in calling of following functions:
onPause() -> onStop() -> onDestroyView()
and when called back to the view it calls following functions
onCreateView() -> onActivityCreated() -> onStop() -> onResume()
Hence the observer is called again in onActivityCreated() method. If you want your observer should not be called again, you should add it in onCreate() method of the respective fragment.