I am trying to create an application that displays a book in portrait and landscape modes. Thus, it is obvious that 1 page is shown in the portrait and 2 in the landscape. Each of the modes works very well, but when I change the orientation from portrait to landscape, viewPager tries to get 2 pages from the portrait, instead of trying to get two double pages from landscape mode ... In fact, FragmentStatePagerAdapter supports 2 fragments created in the portrait, and uses them before creating 2 two-page pages for landscape mode . If I switch again in portrait mode, the FragmentStatePagerAdapter uses 2 fragments previously created in landscape mode, so I see again 1 double page instead of 1 single page, etc. If I continue to switch the orientation, I get an OutOfMemoryError due to the fact that the FragmentStatePagerAdapter will never clear the fragments from changing the orientation.
Here are 2 use cases for easier understanding:
- I launch viewPager in portrait mode
- i see page 1 ok
- I will scroll to the right and I see page 2 ok
- I will scroll to the right and I see page 3 ok
- i turn the screen into landscape mode
- I see page 3 wrong
- I will scroll to the right and I see page 4 incorrectly
i swipe right and I’ll see pages 5 and 6 ok
i launches viewPager in portrait mode
- i see page 1 ok
- I will scroll to the right and I see page 2 ok
- I will scroll to the right and I see page 3 ok
- I will scroll to the right and I see page 4 ok
- I will scroll to the right and I see page 5 ok
- i turn the screen into landscape mode
- I see page 5 wrong
- I scroll left and I see page 4 incorrectly
i swipe left and i see pages 2 and 3 ok
public class PlayerFragmentActivity extends FragmentActivity { private Intent mIntent; private ViewPager mPortraitPager; private ViewPager mLandscapePager; private Boolean mIsLandscape; private String mKey; private int mNbPages; private int mNbDoublePages; private PageFactory mPageFactory; private DoublePageFactory mDoublePageFactory; private PagerAdapter mPagerAdapter; @Override protected void onStop(){ super.onStop(); mPagerAdapter = null; mDoublePageFactory = null; mPageFactory = null; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_activity_player); mIntent = getIntent(); mKey = mIntent.getStringExtra("key"); mNbPages = mIntent.getIntExtra("nbPages", 0); mNbDoublePages = DoublePageFactory.getDoublePageNumFromPageNum(mNbPages); Resources res = getResources(); mIsLandscape = (res.getConfiguration().orientation == 1) ? false : true; mPortraitPager = (ViewPager) findViewById(R.id.portraitPager); mLandscapePager = (ViewPager) findViewById(R.id.landscapePager); mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager()); if (mIsLandscape) { mDoublePageFactory = new DoublePageFactory(this, mKey, mNbPages, res.getInteger(R.integer.nb_page_columns), res.getInteger(R.integer.nb_page_columns)); mPortraitPager.setVisibility(View.GONE); mLandscapePager.setAdapter(mPagerAdapter); mPortraitPager.setAdapter(null); } else { mPageFactory = new PageFactory(this, mKey, this.mNbPages, res.getInteger(R.integer.nb_page_columns), res.getInteger(R.integer.nb_page_columns)); mLandscapePager.setVisibility(View.GONE); mPortraitPager.setAdapter(mPagerAdapter); mLandscapePager.setAdapter(null); } } public class MyPagerAdapter extends FragmentStatePagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Bundle args = new Bundle(); if(mIsLandscape){ Fragment doublePageFragment = new DoublePageFragment(); args.putInt(DoublePageFragment.ARG_DOUBLEPAGE_NUM, position + 1); doublePageFragment.setArguments(args); return doublePageFragment; }else{ Fragment pageFragment = new PageFragment(); args.putInt(PageFragment.ARG_PAGE_NUM, position + 1); pageFragment.setArguments(args); return pageFragment; } } @Override public int getCount() { return (mIsLandscape) ? mNbDoublePages:mNbPages; } @Override public void destroyItem(ViewGroup container, int position, Object object) { FragmentManager manager = ((Fragment) object).getFragmentManager(); FragmentTransaction trans = manager.beginTransaction(); trans.remove((Fragment) object); trans.commit(); super.destroyItem(container, position, object); } @Override public int getItemPosition(Object object){ return PagerAdapter.POSITION_NONE; } @Override public CharSequence getPageTitle(int position) { return "p." + position + 1; } } public boolean isLandscape() { return mIsLandscape; } public ImageView getSinglePage(int position) { return mPageFactory.getPage(position); } public LinearLayout getDoublePage(int position) { return mDoublePageFactory.getDoublePage(position); } } public class PageFragment extends Fragment { private PlayerFragmentActivity mPlayerFragmentActivity; public static final String ARG_PAGE_NUM = "page_number"; public static final String ARG_WOBOOK_DIRECTORY = "book_directory"; public static final String ARG_NB_PAGE_COLUMNS = "nb_page_columns"; public static final String ARG_NB_PAGE_ROWS = "nb_page_rows"; @Override public ImageView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mPlayerFragmentActivity = ((PlayerFragmentActivity) getActivity()); return mPlayerFragmentActivity.getSinglePage(getArguments().getInt(ARG_PAGE_NUM)); } @Override public void onStop(){ super.onStop(); mPlayerFragmentActivity = null; }
}
public class DoublePageFragment extends Fragment { private PlayerFragmentActivity mPlayerFragmentActivity; public static final String ARG_DOUBLEPAGE_NUM = "double_page_number"; public static final String ARG_WOBOOK_DIRECTORY = "book_directory"; public static final String ARG_NB_PAGE_COLUMNS = "nb_page_columns"; public static final String ARG_NB_PAGE_ROWS = "nb_page_rows"; @Override public LinearLayout onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mPlayerFragmentActivity = ((PlayerFragmentActivity) getActivity()); return mPlayerFragmentActivity.getDoublePage(getArguments().getInt(ARG_DOUBLEPAGE_NUM)); } @Override public void onStop(){ super.onStop(); mPlayerFragmentActivity = null; } }
I read that the FragmentManager in the structure takes care of preserving the state and restoring any active fragments that the pager made. So I assume that when the orientation changes, the FragmentManager says
Hey! I already have 2 elements, so I do not need to create news, allows you to get them
But actually it gets 2 elements (page / double page) from the old orientation mode. Therefore, I need an fnd way to remove fragments saved by the FragmentManager when the orientation changes.
I also tested this method . The problem seems to be related to this
Best wishes
android android-fragments android-viewpager screen-orientation fragment
popov130
source share