Fragment setMaxLifecycle方法

前言

ViewPager2中最新的Fragment代码淘汰了setUserVisibleHint方法,转而支持用setMaxLifecycle方法,setMaxLifecycle言外之意是设置最大生命周期,懂行的人应该知道,Fragment一直都是无法直接设置生命周期,必须通过addattachremovedetachshowhide方法间接干预,本来就此功能,简单介绍一下setMaxLifecycle的原理和上手效果;

基本介绍

setMaxLifecycle定义在FragmentTransaction中,和之前的addattachremovedetachshowhide等方法是并列关系;

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,所以该方法可用的参数有CREATEDSTARTEDRESUMEDState生命周期方法有何区别,下面简单解释一下:

生命周期状态理解

在Fragment中,定义了五种State,这里的State并非上面说Lifecycle.State,但是逻辑基本上是一致的;

  • INITIALIZING 初始状态

  • CREATED 已创建状态

  • ACTIVITY_CREATED 完全创建,但是没有started

  • STARTED 创建并启动,可见不可操作

  • RESUMED 创建启动并可操作

本文内容只对CREATEDSTARTEDRESUMED这三个状态讲解,由于Fragment中定义的mStateLifecycle.State不是同一状态,在本文视为同一概念;

与生命周期对应关系

各位肯定都知道Fragment生命周期有onDestoryonStop等方法,但是状态却没有这么多,那么如何标识状态和对应关系,下面给出对应关系;

首先,我把生命周期方法从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();

640?wx_fmt=other

add配合setMaxLifecycle(Lifecycle.State.CREATED)

FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,cardFragment);
fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.CREATED);
fragmentTransaction.commit();

640?wx_fmt=other

add配合setMaxLifecycle(Lifecycle.State.STARTED)

FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout, cardFragment);
fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.STARTED);
fragmentTransaction.commit();

640?wx_fmt=other

add配合setMaxLifecycle(Lifecycle.State.RESUMED)

FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,cardFragment);
fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.RESUMED);
fragmentTransaction.commit();

640?wx_fmt=other

单独使用setMaxLifecycle

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setMaxLifecycle(cardFragment, xxx);
fragmentTransaction.commit();
  • RESUMED状态的Fragment进行操作:CREATED操作

640?wx_fmt=other

  • RESUMED状态的Fragment进行操作:STARTED操作

640?wx_fmt=other

  • RESUMED状态的Fragment进行操作:CREATED操作,在进行STARTED操作

640?wx_fmt=other

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来控制setUserVisibleHintsetMaxLifecycle二选一的局面; mBehavior在构造方法中指定

640?wx_fmt=other

从代码可以看出,用setMaxLifecycle(mCurrentPrimaryItem, Lifecycle.State.STARTED)替代setUserVisibleHint(false),用setMaxLifecycle(fragment, Lifecycle.State.RESUMED)替代setUserVisibleHint(true)

为什么要用Lifecycle.State.STARTED?因为这里本质上用的是add+Lifecycle.State.STARTEDattach+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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值