前言
ViewPager2中最新的Fragment
代码淘汰了setUserVisibleHint
方法,转而支持用setMaxLifecycle
方法,setMaxLifecycle
言外之意是设置最大生命周期
,懂行的人应该知道,Fragment
一直都是无法直接设置生命周期,必须通过add
、attach
、remove
、detach
、show
、hide
方法间接干预,本来就此功能,简单介绍一下setMaxLifecycle
的原理和上手效果;
基本介绍
setMaxLifecycle
定义在FragmentTransaction
中,和之前的add
、attach
、remove
、detach
、show
、hide
等方法是并列关系;
FragmentTransaction
public FragmentTransaction setMaxLifecycle(@NonNull Fragment fragment, @NonNull Lifecycle.State state) {
addOp(new Op(OP_SET_MAX_LIFECYCLE, fragment, state));
return this;
}
参数解读:
-
fragment
即需要操作的Fragment对象,前提条件是这个fragment
必须已经加到FragmentManager
中; -
state
Lifecycle.State
枚举类型,该参数的使用条件是至少是Lifecycle.State.CREATED
,否则报IllegalArgumentException
异常
Lifecycle.State
一共有五个状态,最低要求是Lifecycle.State.CREATED
,所以该方法可用的参数有CREATED
、STARTED
、RESUMED
,State
和生命周期方法
有何区别,下面简单解释一下:
生命周期状态理解
在Fragment中,定义了五种State
,这里的State
并非上面说Lifecycle.State
,但是逻辑基本上是一致的;
INITIALIZING
初始状态
CREATED
已创建状态
ACTIVITY_CREATED
完全创建,但是没有started
STARTED
创建并启动,可见不可操作
RESUMED
创建启动并可操作
本文内容只对CREATED
、STARTED
、RESUMED
这三个状态讲解,由于Fragment中定义的mState
和Lifecycle.State
不是同一状态,在本文视为同一概念;
与生命周期对应关系
各位肯定都知道Fragment生命周期有onDestory
,onStop
等方法,但是状态却没有这么多,那么如何标识状态和对应关系,下面给出对应关系;
首先,我把生命周期方法从onCreate
->onCretateView
->onStart
->onResume
->onPause
->onStop
-> onDestoryView
->onDestory
视为从小到大排序;
同样的,我们把生命周期状态CREATED
->STARTED
->RESUMED
视为从小到大排序;
-
CREATED状态
CREATED
即已创建状态,狭义的理解是生命周期方法走到onCreate
,如果当前fragment状态已大于CREATED
,则会使fragment生命周期方法走到onDestoryView
,如果小于CREATED
,则走到onCreate
;所以CREATED
有两种情况;
-
STARTED状态
同理,STARTED
状态也有两种情况,如果当前fragment状态已大于STARTED
,则会使fragment生命周期方法走到onPause
,如果小于CREATED
,则走到onStart
;
-
RESUMED状态
RESUMED
表示的状态比较特殊,只代表onResume
状态,无论大到小还是小到大,最终都是停留到onResume
状态;
以上生命周期状态扭转结论基于FragmentManagerImpl.moveToState()
方法提取,如有误导,请指教
如何使用
setMaxLifecycle
可以单独使用,也可以配合add
等方法组合使用,首先,我们分析单独执行add
命令的状态变化:
单独执行add操作
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,cardFragment);
fragmentTransaction.commit();
add配合setMaxLifecycle(Lifecycle.State.CREATED)
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,cardFragment);
fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.CREATED);
fragmentTransaction.commit();
add配合setMaxLifecycle(Lifecycle.State.STARTED)
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout, cardFragment);
fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.STARTED);
fragmentTransaction.commit();
add配合setMaxLifecycle(Lifecycle.State.RESUMED)
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,cardFragment);
fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.RESUMED);
fragmentTransaction.commit();
单独使用setMaxLifecycle
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setMaxLifecycle(cardFragment, xxx);
fragmentTransaction.commit();
-
对
RESUMED
状态的Fragment进行操作:CREATED
操作
-
对
RESUMED
状态的Fragment进行操作:STARTED
操作
-
对
RESUMED
状态的Fragment进行操作:CREATED
操作,在进行STARTED
操作
FragmentPagerAdapter变动
由于setMaxLifecycle
带来了生命周期设置,替换掉了老旧的setUserVisibleHint
方法,所以在FragmentPagerAdapter
中也进行了适配
FragmentPagerAdapter
public static final int BEHAVIOR_SET_USER_VISIBLE_HINT = 0;public static final int BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT = 1;private final int mBehavior;public FragmentPagerAdapter(@NonNull FragmentManager fm) { this(fm, BEHAVIOR_SET_USER_VISIBLE_HINT);}public FragmentPagerAdapter(@NonNull FragmentManager fm,@Behavior int behavior) { mFragmentManager = fm; mBehavior = behavior;}复制代码
最新的FragmentPagerAdapter
用一个mBehavior
来控制setUserVisibleHint
和setMaxLifecycle
二选一的局面; mBehavior
在构造方法中指定
从代码可以看出,用setMaxLifecycle(mCurrentPrimaryItem, Lifecycle.State.STARTED)
替代setUserVisibleHint(false)
,用setMaxLifecycle(fragment, Lifecycle.State.RESUMED)
替代setUserVisibleHint(true)
;
为什么要用Lifecycle.State.STARTED
?因为这里本质上用的是add
+Lifecycle.State.STARTED
和attach
+Lifecycle.State.STARTED
组合;
最终的结果是不可见的Fragment只会走到生命周期onStart
方法,不会走onResume
方法;
懒加载新方案
综上,过去使用setUserVisibleHint
来控制Fragment懒加载,在最新版的FragmentPagerAdapter里有新思路,可以切换到BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
模式,在FragmentonResume
里判断,更符合显示逻辑;
切换到BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
模式,需要调用俩参数的构造方法:
new FragmentPagerAdapter(getSupportFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)