I want to detect when a particular fragment of three fragments I have set up with a ViewPager comes on to screen. I have 3 fragments in the viewpager and have set up the viewpager with
mViewPager.setOffscreenPageLimit(2);
so all three fragments are constructed at once (since it stores 2 on each side). I checked that Fragment.onResume() is called just once and not when each fragment comes to the foreground. Is there another callback to ensure I can catch this event and send some analytics signal for it?
Thanks
ViewPager source code:
So, you can use the
ViewPager.addOnAdapterChangeListener
to listen the callback.You can use the
ViewPager.setOnPageChangeListener()
to set aViewPager.OnPageChangeListener()
.Use the listener for the selected page based on its position in the PagerAdapter.
Here's my sample app: