当前位置: 首页 > news >正文

Android:FragmentTransaction

上一篇Android:FragmentTransaction我们大概介绍了FragmentManager的大致工作流程,知道了每个动作都会添加到Op队列里,并由FragmentTransaction进行管理,那么我们就来看看FragmentTransaction的具体内容。

首先FragmentTransaction中定义了每个动作的常量值。

    static final int OP_NULL = 0;static final int OP_ADD = 1;static final int OP_REPLACE = 2;static final int OP_REMOVE = 3;static final int OP_HIDE = 4;static final int OP_SHOW = 5;static final int OP_DETACH = 6;static final int OP_ATTACH = 7;static final int OP_SET_PRIMARY_NAV = 8;static final int OP_UNSET_PRIMARY_NAV = 9;static final int OP_SET_MAX_LIFECYCLE = 10;

在这些值里面,我们看到了非常熟悉的一些操作,比如add,hide,replace等,每个动作都对应一个值。

有了值,就会为每个动作分配定义一个对象。内部类Op。

    static final class Op {//每个动作对应的命令数值int mCmd;//要操作的那个fragmentFragment mFragment;//进入动画int mEnterAnim;//退出时动画int mExitAnim;int mPopEnterAnim;int mPopExitAnim;//声明周期状态Lifecycle.State mOldMaxState;Lifecycle.State mCurrentMaxState;}

那每个动作是如何添加到事务的呢?当我们调用事务的add,hide,show等操作时,从前面文章知道:

        FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction transaction = fragmentManager.beginTransaction();

这个transaction其实是BackStackRecord对象,所以会调用BackStackRecord中对应的方法,比如hide操作。

//BackStackRecord.javapublic FragmentTransaction hide(@NonNull Fragment fragment) {......//调用父类return super.hide(fragment);}//父类:FragmentTransaction.javapublic FragmentTransaction hide(@NonNull Fragment fragment) {addOp(new Op(OP_HIDE, fragment));return this;}

最终调用父类的addOp方法,创建了一个Op对象,并传入OP_HIDE值和要操作的Fragment对象,然后将其添加到mOps列表中。

    ArrayList<Op> mOps = new ArrayList<>();void addOp(Op op) {mOps.add(op);op.mEnterAnim = mEnterAnim;op.mExitAnim = mExitAnim;op.mPopEnterAnim = mPopEnterAnim;op.mPopExitAnim = mPopExitAnim;}

我们在添加fragment到事务时,add方法有两个重载方法。

transaction.add(f1,"f1");
transaction.add(R.id.layout_f,f1,"f1");

一个是不传容器id,一个需要传,第一个方法,其实就是调用第二个方法,然后id传0。

    public FragmentTransaction add(@NonNull Fragment fragment, @Nullable String tag)  {doAddOp(0, fragment, tag, OP_ADD);return this;}

对于第二个方法,我们来看看。

void doAddOp(int containerViewId, Fragment fragment, @Nullable String tag, int opcmd) {//反射final Class<?> fragmentClass = fragment.getClass();final int modifiers = fragmentClass.getModifiers();//检查fragment是否是匿名类,是不是public的,是不是静态类,是不是嵌套类等if (fragmentClass.isAnonymousClass() || !Modifier.isPublic(modifiers)|| (fragmentClass.isMemberClass() && !Modifier.isStatic(modifiers))) {throw new IllegalStateException("Fragment " + fragmentClass.getCanonicalName()+ " must be a public static class to be  properly recreated from"+ " instance state.");}//如果有标签tagif (tag != null) {//检查tag,不允许修改已经存在的tagif (fragment.mTag != null && !tag.equals(fragment.mTag)) {throw new IllegalStateException("Can't change tag of fragment "+ fragment + ": was " + fragment.mTag+ " now " + tag);}fragment.mTag = tag;}//如果设置里容器Idif (containerViewId != 0) {//如果没有为容器设置IDif (containerViewId == View.NO_ID) {throw new IllegalArgumentException("Can't add fragment "+ fragment + " with tag " + tag + " to container view with no id");}//如果fragment已经有容器Id了,不允许变更if (fragment.mFragmentId != 0 && fragment.mFragmentId != containerViewId) {throw new IllegalStateException("Can't change container ID of fragment "+ fragment + ": was " + fragment.mFragmentId+ " now " + containerViewId);}fragment.mContainerId = fragment.mFragmentId = containerViewId;}//添加Op到队列addOp(new FragmentTransaction.Op(opcmd, fragment));
}

代码做了注释,就不在具体描述了。

再来看attach和detach方法,detach会将fragment从UI界面移除,即使调用show也无法再次显示出来。调用attach后,fragment会重新显示在界面上。

        findViewById(R.id.tv_1).setOnClickListener((v -> {FragmentTransaction t = fragmentManager.beginTransaction();t.detach(f1);t.commitNow();}));findViewById(R.id.tv_2).setOnClickListener((v -> {FragmentTransaction t = fragmentManager.beginTransaction();t.attach(f1);t.commitNow();}));

http://www.lryc.cn/news/247605.html

相关文章:

  • 5.golang字符串的拆解和拼接
  • 配置 Mantis 在 Windows 上的步骤
  • Android 单元测试初体验(二)-断言
  • 通过ros系统中websocket中发送sensor_msgs::Image数据给web端显示
  • 【 Kubernetes 风云录 】- Istio 应用多版本流量控制
  • 比尔盖茨:GPT-5不会比GPT-4好多少,生成式AI已达到极限
  • let const 与var的区别
  • git 把项目托管到码云
  • sCrypt 现已支持各类主流前端框架
  • leetcode:2549. 统计桌面上的不同数字(python3解法)
  • 数据结构 / day03作业
  • 异步爬虫提速实践-在Scrapy中使用Aiohttp/Trio
  • Python与设计模式--访问者模式
  • 为社会做贡献的EasyDarwin 4.0.1发布了,支持视频点播、文件直播、摄像机直播、直播录像、直播回放、录像MP4合成下载
  • CG向量和矩阵元素的获取
  • 牛客 算法题 golang语言实现
  • 鸿蒙开发报错:agconnect sdk not initialized. please call initialize()【BUG已解决】
  • 极智芯 | 解读国产AI算力算能产品矩阵
  • docker介绍、部署与常用命令
  • windows定时任务命令工具schtasks
  • 多个nginx共享值、缓存问题
  • 【2023传智杯】第六届传智杯程序设计挑战赛AB组-DEF题解题分析详解【JavaPythonC++解题笔记】
  • Spark---SparkCore(五)
  • k8s中pod的hostport端口突然无法访问故障处理
  • 高德开始“跑腿”
  • Notion for Mac:打造您的专属多功能办公笔记软件
  • pip 安装软件出现 [No space left on device]
  • 【算法刷题】Day8
  • 基于单片机的智能饮水机控制系统(论文+源码)
  • 电脑格式化了怎么恢复原来的数据?您可以这样做