一、起源:

先看效果,在linearLayout中添加了4个Fragment,然后点击替换一次确替换了两个Fragment,引发了我的研究兴趣;

第一次启动                                  点击一次                         点击两次                              点击三次

 

代码很简单  activity  onCreate 方法中添加了4个Fragment

FragmentTransaction transaction =manager.beginTransaction();

transaction.add(R.id.content,fragment1,"a");

transaction.add(R.id.content,fragment1_2,"b");

transaction.add(R.id.content,fragment1_3,"c");

transaction.add(R.id.content,fragment1_4,"d");

transaction.commit();

replace 按钮监听事件中添加了如下代码

Fragment2 fragment2_1 =newFragment2();

FragmentTransaction transaction=manager.beginTransaction();

transaction.replace(R.id.content,fragment2_1,"kk");

transaction.commit();

二、探究transaction.replace到底做了什么

探究源码得知FragmentTransaction 对象是在FragmentManagerImpl 类中的beginTransaction()方法中产生的;

@Override

publicFragmentTransaction beginTransaction() {

returnnewBackStackRecord(this);

}

这才发现BackStackRecord产生的对象才是我们真正使用的FragmentTransaction,那BackStackRecord.replace()  方法究竟做了啥,让我们一探究竟;

publicFragmentTransactionreplace(intcontainerViewId, Fragment fragment, String tag) {

doAddOp(containerViewId, fragment, tag,OP_REPLACE);

return this;

}

public FragmentTransactionadd(intcontainerViewId, Fragment fragment, String tag) {

doAddOp(containerViewId, fragment, tag,OP_ADD);

return this;

}

可以看到add和 replace  方法都没有自己去处理而是交给doAddOp处理,doAddOp()简化代码如下

privatevoiddoAddOp(intcontainerViewId, Fragment fragment, String tag,int opcmd){

fragment.mFragmentManager = mManager;

if (tag!=null) {

fragment.mTag = tag;

}

if(containerViewId != 0) {

fragment.mContainerId = fragment.mFragmentId =containerViewId;

}

Op op =new Op();

op.cmd =opcmd;

op.fragment =fragment;

addOp(op);

}

我们发现,add  和replace  方法都是进行了fragment对象的tag、mFragmentId、mContainerId的赋值,mContainerId是容器id,也就是说每一个fragment 都有保留它被添加的容器的id,也就是我们replace传入的R.id,content;

再看看OP

static final class Op {

Op next;

Op prev;

int cmd;

Fragment fragment;

int enterAnim;

int exitAnim;

int popEnterAnim;

int popExitAnim;

ArrayList<Fragment> removed;

}
Op其实就是保存我们处理动作的一个对象,经过一系列追踪发现,最终在BackStackRecord.run()中处理了这个对象;具体的追踪过程可以参考:https://zhuanlan.zhihu.com/p/20660984

处理源码如下:

switch (op.cmd) {

caseOP_ADD: {

Fragment f = op.fragment;

f.mNextAnim = op.enterAnim;

mManager.addFragment(f,false);

break;

caseOP_REPLACE: {

Fragment f = op.fragment;

if (mManager.mAdded !=null) {

for (int i=0;i<mManager.mAdded.size(); i++) {

Fragment old =mManager.mAdded.get(i);

if(FragmentManagerImpl.DEBUG) Log.v(TAG,

"OP_REPLACE: adding=" + f + "old=" + old);

if (f == null ||old.mContainerId == f.mContainerId) {

if (old== f) {

op.fragment = f =null;

else {

if (op.removed ==null) {

op.removed =newArrayList<Fragment>();

}

op.removed.add(old);

old.mNextAnim = op.exitAnim;

if (mAddToBackStack) {

old.mBackStackNesting += 1;

if(FragmentManagerImpl.DEBUG) Log.v(TAG,"Bump nesting of "

+ old +" to " + old.mBackStackNesting);

}

mManager.removeFragment(old,mTransition,mTransitionStyle);

}

}

}

}

if (f !=null) {

f.mNextAnim = op.enterAnim;

mManager.addFragment(f,false);

}

break;

好了终于找到replace的真正处理之处,我们精练出关键代码再看看:

switch (op.cmd) {

caseOP_ADD: {

Fragment f = op.fragment;

mManager.addFragment(f,false);

break;

caseOP_REPLACE: {

Fragment f = op.fragment;

if (mManager.mAdded !=null) {

for (int i=0;i<mManager.mAdded.size(); i++) {

Fragment old =mManager.mAdded.get(i);

if (f == null ||old.mContainerId == f.mContainerId) {

if (old== f) {

op.fragment = f =null;

else {

mManager.removeFragment(old,mTransition,mTransitionStyle);

}

}

}

}

if (f !=null) {

mManager.addFragment(f,false);

}

break;

可以看到

1、add方法就是调用了fragmentmanager的添加方法;

2、replace 则是先删除fragmentmanager中所有已添加的fragment中,容器id与当前要添加的fragment的容器id相同的fragment;然后再添加当前fragment;

3、由于添加的时候都是在一个LinearLayout 中,那么所有的 fragment的容器Id都是一样的;

得出结论:  replace 会删除LinearLayout中所有fragment  ,然后再添加传入fragment对象;

好,问题来了,最开始的图片点击第一次删除的是fragment1和fragment1_3  ,第二次只删除了fragment1_3,并没有删除全部,这又是为什么;

带着疑问的态度进行了一次调试,在调试中终于找到了原因,问题就在这段代码:

for (int i=0; i<mManager.mAdded.size(); i++) {

Fragment old = mManager.mAdded.get(i);

if (f ==null ||old.mContainerId == f.mContainerId) {

mManager.removeFragment(old,mTransition, mTransitionStyle);

}

}

mManager.mAdded  是一个ArrayList<Fragment>  列表,在遍历的时候调用了mManager.removeFragment方法,而该方法调用了ArrayList的remove方法;

public void removeFragment(Fragmentfragment, int transition, inttransitionStyle) {

mAdded.remove(fragment);

}

也就是说在用for循环遍历ArrayList列表的时候使用了remove;这是开始怀恋我们的Java老师的了,list遍历列表要删除元素我们要用iterator.remove();

For循环遍历过程删除会造成ArrayList.size()不断变小,所以造成删除不完全的问题;你是否也被坑过。。。

笔记建议  Android此处可以将 mManager.mAdded复制一份再遍历,就不会有这个问题了(亲测有效);

ArrayList<Fragment> list=new ArrayList<Fragment>(mManager.mAdded )  ;    
                for (int i=0; i<list.size();i++) {
                          Fragment old = list.get(i);

if (f ==null ||old.mContainerId == f.mContainerId) {

mManager.removeFragment(old,mTransition, mTransitionStyle);

}

}

三、总结

 用于我们常常使用FrameLayout 做容器fragment都掩盖了下面其他Fragment,大部分情况下看不到此类问题,看不到不表示不存在,笔者建议,遇到此类还是手动去调用remove+add方法,一定要使用replace()可以去修改源码,如果你不嫌麻烦的话。。。

FragmentTransaction.replace() 你不知道的坑的更多相关文章

  1. 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)

    即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...

  2. 解决fragmentTransaction.replace不能全屏

    今天遇到个问题,使用fragmentTransaction.replace替换后的内容不能全屏.. FragmentManager fragmentManager = getSupportFragme ...

  3. replace的坑

    问题:html中代码段包含了$,在使用replace替换时,$直接被替换了解决:先把文本中的$全部替换成自己定义的标签,最后在还原回去原因:在介绍replace的文档中,$&代表插入匹配的子串 ...

  4. java调用第三方命令,process.waitfor()挂起(你不知道的坑)

    我们常在java中运行第三方程序,如sh.python,java提供一个Runtime.exec()方法,生成一个Process对象.今天在使用这个方法的时候,发现接口半天没有返回数据.查了一下,原来 ...

  5. MySQL 主从同步架构中你不知道的“坑”(2)

    指定同步库情况 1.binlog_format= ROW模式‍ mysql> use testdb; Database changed mysql> show tables; +----- ...

  6. MySQL 主从同步架构中你不知道的“坑”

    以下操作征对指定不同步库 binlog-format=ROW模式 1 查看主从的binlog模式 mysql> show slave status\G ********************* ...

  7. LVS+Keepalived深度理解,阐述你不知道的坑点

    1. LVS简介 1. 什么是LVS? LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内 ...

  8. 错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)

    Fragment newfragment =new MyFragment();fragmentTransaction.replace(R.layout.activity_main,newfragmen ...

  9. FragmentTransaction add 和 replace 区别 转

    使用 FragmentTransaction 的时候,它提供了这样两个方法,一个 add , 一个 replace . add 和 replace 影响的只是界面,而控制回退的,是事务. public ...

随机推荐

  1. 编写高质量的Objective-C代码

    点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问,其他的情况使用方括号标记语法.   良好的风格: view.backgroundColor = [UIColor or ...

  2. Phonegap 之 iOS银联在线支付(js调用ios端银联支付控件)

    Phonegap项目,做支付的时候,当把网站打包到ios或android端成app后,在app上通过wap调用银联在线存在一个问题: 就是当从银联支付成功后,再从服务器返回到app客户端就很难实现. ...

  3. Android studio .gitignore 文件的内容

    # built application files *.apk *.ap_ # files for the dex VM *.dex # Intellij project files .idea/ . ...

  4. cocoaPods框架管理工具使用

    前言:文中的"$"表示命令行,使用使不需要包含 cocoaPods的安装 因为cocoaPods需要用Gem进行安装,所以如果系统比较旧需要先升级Gem $ sudo gem up ...

  5. iOS_UITableView性能优化那些事

    UITableView在实际开发中使用频率实在是很高, 因此, UITableView的性能优化是必不可少的, 本文下面就略微总结一下UITableView性能优化那些事. 本文着重介绍具体方法, 原 ...

  6. C++ virtual虚函数

    #include<iostream> using namespace std; class Base{ public: void m() { cout << "it' ...

  7. iOS带动画的环形进度条(进度条和数字同步)

    本篇写的是实现环形进度条,并带动画效果,要实现这些,仅能通过自己画一个 方法直接看代码 为了方便多次调用,用继承UIView的方式 .m文件 #import <UIKit/UIKit.h> ...

  8. IOS开发之开发者账号遇到的bug

    今天使用开发者账号过期的问题,文件显示 其实今天的问题和这个没有关系,即使上面显示此证书的签发者无效,有时候也是可以用的. 我这里情况比较奇葩,刚刚生成的开发者账号,显示还是"......无 ...

  9. display:inline-block 去除间隙

    先写出代码 核心css代码: .sample0{display: inline-block;height: 40px;width: 40px;color: #ffffff;background-col ...

  10. 给现有MVC 项目添加 WebAPI

    1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http 2 ...