I’m using a FragmentStatePageAdapter (android.support.v4) and I have setOffscreenPageLimit set to 2, so it creates and stores Fragments 2 ahead and 2 behind of the currently displayed Fragment.
Problem:
When the off-screen Fragments are created, they are also immediately started and resumed even though they haven’t been painted to the screen yet. (!)
When the current page is changed and the corresponding Fragment is swiped off screen, it isn’t paused or stopped. (!)
I’ve tried logging the behavior of all the callbacks in FSPA and its super class – setPrimaryItem comes the closest to being usable but appears to be called for all sorts of reasons, not just when the fragment is displayed.
How can you detect that one of your Fragments is no longer displayed, or returning to the display?
You could use a listener.
Where PageSelectedListener is defined by you like so
And use it like this in your fragment