I have a PagerAdapter and it has 3 pages. When scrolling the first - the second page is recreated hundreds of times. When you go to the second - the third is recreated. When scrolling the third, the first one is recreated, and, when scrolling, the second one is recreated hundreds of times. How to make the fragments uploaded to the ViewPagger and not recreated?

Here is my adapter:

 public static class MyPagerAdapter extends FragmentPagerAdapter { private static int NUM_ITEMS = 3; public MyPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager); } @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { switch (position) { case 0: return CreatePage_Fragment.newInstance(0); case 1: return CreatePage_Fragment.newInstance(1); case 2: return CreatePage_Fragment.newInstance(2); default: return null; } } @Override public CharSequence getPageTitle(int position) { return "Page " + position; } } 

Here is his appointment:

 FragmentPagerAdapter adapterViewPager = new MyPagerAdapter(getSupportFragmentManager()); viewPager = (ViewPager) findViewById(R.id.main_activity_viewPagger_1); viewPager.setAdapter(adapterViewPager); 

    2 answers 2

    If you mean calling the onCreate() method of fragments located to the left / right of the displayed one, then this is normal behavior. ViewPager loads fragments to the left and right of the current to speed up their display when they change. He does it once for each piece. At the same time, it has a limit for saving fragments to the left / right of the current one. By default it is equal to one. In this case, when moving to 3, fragment 1 is destroyed and loaded again when moving to 2. This can be corrected by setting a larger limit for saving fragments. In your case, fit 2

     mViewPager.setOffscreenPageLimit(2); 

    Where are you from

    the second page is reconstructed hundreds of times

    from your question is not clear. Lay out the fragment code. And explain the "re-created."

    • one
      Everything works correctly! And the next fragment was recreated hundreds of times, when I turned up / down the ListView in the first fragment. I do not know why, but it was gone ...: \ - user189127

    Nothing prevents you from creating an array / map of fragments and transferring it to the adapter, and in the adapter itself, in the getItem () method, do not recreate the fragment, but pull it along the position from the container. It all depends on the size of the array and the feasibility of re-creating fragments.