I'm using ViewPager 2 from AndroidX with 4 instances of the same fragment. My question is pretty straight forward. When I'm navigating to some another fragment(using navigation drawer or even something else). OnStop() , OnDestroy(), OnDettach() of the fragments inside the viewpager does not gets triggered. So why is that? And If I want to remove the listeners I've started already, in one of these methods, how can I do that?
For example, I'm using GreenRobot's EventBus. And I'm registering the EvenBus inside OnStart:
override fun onStart() {
super.onStart()
EventBus.getDefault().register(this)
}
And Removing it from OnStop:
override fun onStop() {
Log.e(TAG, "onStop: ")
EventBus.getDefault().unregister(this)
super.onStop()
}
But when I navigate away from the viewpager as I explained above, onStop does not trigger. I even checked it by logging.
So is the fragment lifecycle works differently with viewpager? And if yes, how can I overcome this problem(unregistering EvetBus).
FragmentPagerAdapter
orFragmentStatePagerAdapter
? – Brio