Q:

FragmentStatePageAdapter Doesn't Pause Fragments

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? 

A:

You could use a listener.
        mPager.setOnPageChangeListener(new OnPageChangeListener(){

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int position) {
                if(mPageSelectedListener!=null){
                    mPageSelectedListener.pageSelected(position);
                }
            }

        });

Where PageSelectedListener is defined by you like so
    public interface PageSelectedListener{
        public void pageSelected(int position);
    };

    public void setPageSelectedListener(PageSelectedListener l){
        mPageSelectedListener = l;
    }

And use it like this in your fragment
    if(getActivity() instanceof MyActivity
        ((MyActivity)getActivity()).setPageSelectedListener(new PageSelectedListener(){
            @Override
            public void pageSelected(int position) {
                if(position==MyAdapter.MY_PAGE){
                    // do something with currently viewed page...like resume it
                } else {
                    // do something with any other page..like pause it
                }
            }
        });
    }

