1

Views (in this case, ViewPagers) have their own implementation of saving and restoring state, and I was relying on it to keep my ViewPager on the same page after a device rotate.

It worked perfectly fine when the ViewPager was in an Activity. I'd scroll to a page, rotate the device, and it'd still be on that page. No code was needed in onSaveInstanceState.

I've since converted that Activity into a fragment. Now, the ViewPager doesn't keep its state! If I scroll to a page and rotate the device, it goes back to page 1.

I know that I can save and restore the page in onSaveInstanceState and onCreateView, but I'd rather let the view do it on its own, especially as I add more views and it becomes more complex.

How do I get back this view restoring functionality with fragments?

Here's my fragment:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Dashboard extends Fragment {

    private PagerAdapter pagerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pagerAdapter = new PagerAdapter(getChildFragmentManager());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dashboard, container, false);

        // irrelevant bits removed

        ViewPager pager = (ViewPager) view.findViewById(R.id.dashboard_pager);
        pager.setOffscreenPageLimit(3);
        pager.setAdapter(pagerAdapter);

        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.dashboard_tabs);
        tabs.setShouldExpand(true);
        tabs.setViewPager(pager);

        return view;
    }

    class PagerAdapter extends FragmentStatePagerAdapter implements PagerSlidingTabStrip.CustomTabProvider {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return Fragment.instantiate(getActivity(), Fragment1.class.getName());
                case 1:
                    return Fragment.instantiate(getActivity(), Fragment2.class.getName());
                case 2:
                    return Fragment.instantiate(getActivity(), Fragment3.class.getName());
            }

            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "*removed*";
                case 1:
                    return "*removed*";
                case 2:
                    return "*removed*";
            }

            return null;
        }

        @Override
        public View getCustomTabView(ViewGroup viewGroup, int i) {
            return getActivity().getLayoutInflater().inflate(R.layout.tab, viewGroup, false);
        }
    }
}
Steven Schoen
  • 4,366
  • 5
  • 34
  • 65
  • 1
    http://stackoverflow.com/a/9646622/609782 have checked this answer? If you wish to find if your fragment is still alive in `onSavedInstanceState()`, you can avoid it. Apart from that I assume you are aware of `android:configChanges= "orientation|screenSize"` ? – Darpan Apr 06 '15 at 03:54
  • Awesome, that put me on the right track! I had to tweak it a bit as I use a navigation drawer, but essentially checking for the fragment by tag and recreating it if it wasn't found seems to be working perfectly. Thanks! If you make an answer I'll accept it. – Steven Schoen Apr 06 '15 at 04:30

1 Answers1

1

Have you checked this answer?

If you find if your fragment is still alive in onSavedInstanceState(), you can avoid it.

Apart from that I assume you are aware of android:configChanges= "orientation|screenSize"

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80