How Does Fragment's Lifecycle Works Inside Viewpager? Why Onstop Is Not Called On Navigation Change?
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 dra
Solution 1:
Unfortunately, EventBus does not provide great usefulness when it comes to ViewPager and Fragments inside it.
Though I found a solution, using the more traditional approach: Interfaces
It does not directly answer the question Why onStop is not called of fragments inside ViewPager on navigation change?
But it does save you from multiple Event triggers when using EvenBus with ViewPager. With interfaces, As you don't have to explicitly unregister the interface. It does not matter if the onStop is called.
Solution 2:
You can use setUserVisibleHint
to check the fragment visibility.
overridefunsetUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser) {
//Fragment is visible
} else {
//Fragment is invisible
}
}
Hope this helps.
Post a Comment for "How Does Fragment's Lifecycle Works Inside Viewpager? Why Onstop Is Not Called On Navigation Change?"