FragmentStatePageAdapter doesn't remove child fragments

146 views Asked by At

Having a problem with FragmentStatePagerAdapter and screen change orientation (saving state I guess). The layout of my app is the following:

Activity -->
         Fragment A
         Fragment B (contains a FragmentStatePagerAdapter) wich shows 2 fragments 
                  --> 
                    Fragment B1 
                    Fragment B2

All the fragments mentioned inherit from a BaseFragment class that doesn't do anything special but keeping some "global" vars I need. When Activity starts loads up Fragment A. From a menu I replace Fragment A with Fragment B. Fragment B source is the following:

public class Fragment B extends BaseFragment {

    public static String TAG = "b_fragment";
    private MyCustomPagerAdapter adapter;
    private ArrayList<String> tags = new ArrayList<>();
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new MyCustomPagerAdapter(getFragmentManager());
        //adapter = new MyCustomPagerAdapter(getChildFragmentManager());
        tags.add(FragmentB1.TAG);
        tags.add(FragmentB2.TAG);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sample, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(adapter);
        SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout)view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setViewPager(mViewPager);        
    }

    class MyCustomPagerAdapter extends FragmentStatePagerAdapter {
    //class MyCustomPagerAdapter extends FragmentPagerAdapter {

        FragmentManager mgr;
        public SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

        public PrepagatePagerAdapter(FragmentManager mgr) {
            super(mgr);
            this.mgr = mgr;
        }

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new FragmentB1();
                case 1:
                    return new FragmentB2();
            }
            return null;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)  {
            Fragment fragment = (Fragment)  super.instantiateItem(container, position);
            registeredFragments.put(position, fragment);
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            registeredFragments.remove(position);
            super.destroyItem(container, position, object);
        }

        public Fragment getRegisteredFragment(int position) {
            return registeredFragments.get(position);
        }
    }
}

The problem happens when I go to Fragment B then go back again to Fragment A and change orientation: I see while debugging on the onAttach() method of Fragment A that in the FragmentManager Fragment B is no more present (replaced by Fragment A) but Fragment B1 and B2 are there and are called their rispective onAttach() methods, resulting in weird behaviours of the layout. Didn't the child fragments should be remove from the FragmentManager?

At the moment I don't know if more details are needed. I can give further if you need.

0

There are 0 answers