Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager behavior, it does not destroy old fragment

I want to know more about ViewPager behavior. I have a FragmenPagerAdapter :

public class DatePagerAdapter extends FragmentPagerAdapter {

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


        @Override
        public Fragment getItem(int position) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            int offset = position - 100;
            calendar.add(Calendar.MONTH, offset);
//            Toast.makeText(TestActivity.this,(calendar.get(Calendar.MONTH)+1)
            Log.v("CALENDAR", "" + position);
            TestFragmentDate date = TestFragmentDate.newInstance(TestActivity.this, calendar);
            return date;
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }
    }

And the code in my Activity :

adapter = new DatePagerAdapter(getSupportFragmentManager());
        MinFragmentPagerAdapter wrapperMin = new MinFragmentPagerAdapter(getSupportFragmentManager());
        wrapperMin.setAdapter(adapter);
        PagerAdapter wrapper = new InfinitePagerAdapter(wrapperMin);
        viewPager = (ViewPager) this.findViewById(R.id.pager);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(100);
        viewPager.addOnPageChangeListener(this);

According to my senior, ViewPager always draw 3 fragments, and keep reuse them. For example, at first I have view at position : 99, 100, 101 If I roll right, it will destroy 99 and create 102, and so on. But, when I debug at the function getItem, at first it did run into this function, but when I roll right for 5 or 6 page, and then roll back, it didn't run into getItem, at the position which is supposed to be destroyed.

So would anyone please explain for me about ViewPager's behavior ? Thank you.

like image 747
Nguyen Quang Anh Avatar asked Dec 04 '22 01:12

Nguyen Quang Anh


1 Answers

Google's guide says:

FragmentPagerAdapter

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.

And about FragmentStatePagerAdapter:

This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

Conclusion : Use FragmentStatePageAdapter in your case, that is you don't want Fragment attached to your ViewPager to be destroyed.

You can also use viewpager.setOffscreenPageLimit(<no of fragments>); to limit How many pages will be kept offscreen in an idle state.

like image 174
Vipul Asri Avatar answered Jan 07 '23 21:01

Vipul Asri