How to properly handle screen rotation with a ViewPager and nested fragments?

前端 未结 6 679
执念已碎
执念已碎 2021-01-31 03:42

I\'ve got this activity, which holds a fragment. This fragment layout consists of a view pager with several fragments (two, actually).

When the view pager is created, it

6条回答
  •  被撕碎了的回忆
    2021-01-31 04:20

    My code :

        public class SampleAdapter extends FragmentStatePagerAdapter {
    
        private Fragment mFragmentAtPos2;
        private FragmentManager mFragmentManager;
        private Fragment[] mFragments = new Fragment[3];
    
    
        public SampleAdapter(FragmentManager mgr) {
            super(mgr);
            mFragmentManager = mgr;
            Bundle hack = new Bundle();
            try {
                for (int i = 0; i < mFragments.length; i++) {
                    hack.putInt("hack", i);
                    mFragments[i] = mFragmentManager.getFragment(hack, "hack");
                }
            } catch (Exception e) {
                // No need to fail here, likely because it's the first creation and mActive is empty
            }
    
        }
    
        public void switchFrag(Fragment frag) {
    
            if (frag == null) {
                Dbg.e(TAG, "- switch(frag) frag is NULL");
                return;
            } else Dbg.v(TAG, "- switch(frag) - frag is " + frag.getClass());
            // We have to check for mFragmentAtPos2 null in case of first time (only Mytrips fragment being instatiante).
            if (mFragmentAtPos2!= null) 
                mFragmentManager.beginTransaction()  
                    .remove(mFragmentAtPos2)
                    .commit();
            mFragmentAtPos2 = frag;
            notifyDataSetChanged();
        }
    
        @Override
        public int getCount() {
            return(3);
        }
    
        @Override
        public int getItemPosition(Object object) {
            Dbg.v(TAG,"getItemPosition : "+object.getClass());
            if (object instanceof MyTripsFragment
                    || object instanceof FindingDriverFragment
                    || object instanceof BookingAcceptedFragment
                    || object instanceof RideStartedFragment
                    || object instanceof RideEndedFragment
                    || object instanceof ContactUsFragment
                    )
                return POSITION_NONE;
            else return POSITION_UNCHANGED;
    
        }
    
        @Override
        public Fragment getItem(int position) {
            Dbg.v("SampleAdapter", "getItem called on: "+position);
            switch (position) {
            case 0: 
                if (snapbookFrag==null) {
                    snapbookFrag = new SnapBookingFragment();
                    Dbg.e(TAG, "snapbookFrag created");
                }
                return snapbookFrag;
            case 1: 
                if(bookingFormFrag==null) {
                    bookingFormFrag = new BookingFormFragment();
                    Dbg.e(TAG, "bookingFormFrag created");
                }
                return bookingFormFrag;
            case 2: 
                if (mFragmentAtPos2 == null) {
                    myTripsFrag = new MyTripsFragment();
                    mFragmentAtPos2 = myTripsFrag;
                    return mFragmentAtPos2;
                }
                return mFragmentAtPos2;
            default:
                return(new SnapBookingFragment());
            }
        }
    }
    

提交回复
热议问题