ViewPager+Fragment生命周期方法(一)

一:ViewPager和Fragment的几个问题

1: Fragment.setUserVisibleHint():
这个方法的含义是当前Fragment是ViewPager的currentPage时,该方法返回true,否则返回false;需要注意的是只有Fragment用在ViewPager中作为page时才会调用该方法,在其他情况下并不会调用;而且该方法在onCreateView()和onCreate()方法之前调用;

setUserVisibleHint

2: FragmentPagerAdater和FragmentStatePagerAdapter的区别:
参考1
(1)FragmentPagerAdater:把整个Fragment存储在内存中,如果有大量的Fragment的话,可能会加大内存的占用;Fragment退出不会执行onDestory()方法来销毁Fragment实例,只会执行onDestoryView()销毁View;
(2)FragmentStatePagerAdapter:Fragment只存储 savedInstanceState;执行onDestory()方法

3:Viewpager.setOffscreenPageLimit()含义
当前页面左右两边保留的Fragment的数量,如果超出这个界限,Fragment会被onDestory()或者onDestoryView(),使用这个方法可以做一些懒加载的处理;

 /**
     * Set the number of pages that should be retained to either side of the
     * current page in the view hierarchy in an idle state. Pages beyond this
     * limit will be recreated from the adapter when needed.
     *
     * 

This is offered as an optimization. If you know in advance the number * of pages you will need to support or have lazy-loading mechanisms in place * on your pages, tweaking this setting can have benefits in perceived smoothness * of paging animations and interaction. If you have a small number of pages (3-4) * that you can keep active all at once, less time will be spent in layout for * newly created view subtrees as the user pages back and forth.

* *

You should keep this limit low, especially if your pages have complex layouts. * This setting defaults to 1.

* * @param limit How many pages will be kept offscreen in an idle state. */ public void setOffscreenPageLimit(int limit) { if (limit < DEFAULT_OFFSCREEN_PAGES) { Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " + DEFAULT_OFFSCREEN_PAGES); limit = DEFAULT_OFFSCREEN_PAGES; } if (limit != mOffscreenPageLimit) { mOffscreenPageLimit = limit; populate(); } }

二:实例证明

情形一:
Activity中有三个Fragment;SetOffscreePageLimit=1;
Adapter采用FragmentStatePagerAdapter

MainActivity

当第一个Fragment为currentPage时,会把第二个Fragment预加载出来;但是由于SetOffscreePageLimit=1,所以并不会加载第三个Fragment;

生命周期

当第二个Fragment为currentPage时,会把第三个Fragment预加载出来;但是由于SetOffscreePageLimit=1,所以第二个Fragment左右两边都能存在一个Fragment,所以此时共存活三个Fragment;

生命周期

当第三个Fragment为currentPage时,由于SetOffscreePageLimit=1,第一个Fragment不在limit的范围之内,所以会销毁第一个Fragment;执行onDestory();

生命周期

情形二:
Activity中有三个Fragment;SetOffscreePageLimit=3;
Adapter采用FragmentStatePagerAdapter
那么所有的Fragment都不会被销毁;

MainActivity

当第一个Fragment为currentPage时,三个Fragment都会被创建


生命周期

来回移动tab也不会导致Fragment销毁,只会影响Fragment的可见性;


生命周期

情形三:Activity中有三个Fragment,SetOffscreePageLimit=1;
Adapter是FragmentPagerAdater

当第一个Fragment为currentPage时,会把第二个Fragment预加载出来;但是由于SetOffscreePageLimit=1,所以并不会加载第三个Fragment;

1

当第二个Fragment为currentPage时,会把第三个Fragment预加载出来;但是由于SetOffscreePageLimit=1,所以第二个Fragment左右两边都能存在一个Fragment,所以此时共存活三个Fragment;

2

当第三个Fragment为currentPage时,由于SetOffscreePageLimit=1,第一个Fragment不在limit的范围之内,所以会销毁第一个Fragment;执行***onDestoryView() ,不是onDestory() ***(此处需要注意);

3

情形四:
Activity中有三个Fragment;SetOffscreePageLimit=3;
Adapter采用FragmentPagerAdapter
那么所有的Fragment都不会被销毁;

你可能感兴趣的:(ViewPager+Fragment生命周期方法(一))