使用 FragmentTransaction 的时候,它提供了这样两个方法,一个 add , 一个 replace .

add 和 replace 影响的只是界面,而控制回退的,是事务。

public abstract FragmentTransaction add (int containerViewId, Fragment fragment, String tag)

Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

add 是把一个fragment添加到一个容器 container 里。

public abstract FragmentTransaction replace (int containerViewId, Fragment fragment, String tag)

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

replace 是先remove掉相同id的所有fragment,然后在add当前的这个fragment。

在大部分情况下,这两个的表现基本相同。因为,一般,咱们会使用一个FrameLayout来当容器,而每个Fragment被add 或者 replace 到这个FrameLayout的时候,都是显示在最上层的。所以你看到的界面都是一样的。但是, 使用add的情况下,这个FrameLayout其实有2层,多层肯定要比一层的来得浪费,所以还是推荐使用replace。 当然有时候还是需要使用add的。比如要实现轮播图的效果,每个轮播图都是一个独立的Fragment,而他的容器FrameLayout需要add多个Fragment,这样他就可以根据提供的逻辑进行轮播了。

而至于返回键的时候,这个跟事务有关,跟使用add还是replace没有任何关系。

add 在添加第一个项目是,会直接替换掉原来的,而replace+addbackstack方式,会导致内容在回退的时候,显示白屏。

FragmentTransaction add 和 replace 区别 转的更多相关文章

  1. Fragment add replace 区别

    首先获取FragmentTransaction对象:FragmentTransaction transaction = getFragmentManager().beginTransaction(); ...

  2. Fragment 源码解析add()和replace()方法

    1.有问题的代码: MainActivity public class MainActivity extends AppCompatActivity implements View.OnClickLi ...

  3. Maste Note for OCR / Vote disk Maintenance Operations (ADD/REMOVE/REPLACE/MOVE)

    Doc ID 428681.1 Applies to: Oracle Database - Enterprise Edition - Version 10.2.0.1 to 11.2.0.1.0 [R ...

  4. Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:25:5-27:41 to override.

    记录下来少走些坑吧 一:不管用 tools:replace="android:icon,android:theme" xmlns:tools="http://schema ...

  5. flutter——android报错建议Suggestion: add 'tools:replace="android:label"'

    问题: 安装了一个新包,android出现了报错,建议add 'tools:replace="android:label"'. 原因: 项目application的label属性冲 ...

  6. Fragment 的 replace 和 add 方法的区别?

    Fragment 本身并没有 replace 和 add 方法,这里的理解应该为使用 FragmentManager 的 replace 和 add 两种方法切换 Fragment 时有什么不同.我们 ...

  7. TStringList TMemo Text与Add赋值的区别 Memo.Text赋值高度注意事项,不得不知的技巧。

    Memo.Text赋值高度注意事项,不得不知的技巧. list := TStringList.Create;  list.Text:= str:  list.Count; list.Clear;  l ...

  8. mysql 索引查询 、创建 create index 与 add index 的区别

    1.索引查询 ------TABLE_SCHEMA  库名:TABLE  表名 ------AND UPPER(INDEX_NAME) != 'PRIMARY'  只查询索引,不需要主键 SELECT ...

  9. window.location.href 与 window.loaction.replace区别

    window.location.href和window.location.replace的区别 1.window.location.href=“url”:改变url地址: 2.window.locat ...

随机推荐

  1. thinkphp查询:

    $Role=D('role'); //查询数据表 $role_data = $Role->order('role_id')->group('role_name')->select() ...

  2. day 3 模块

    1.系统自带模块  xxx.py 文件 就是模块 ### 模块存放位置 In [1]: import os In [2]: os.__file__ Out[2]: '/usr/lib/python3. ...

  3. 【LG3835】可持久化平衡树

    [LG3835]可持久化平衡树 题面 洛谷 解法一 参考文章 rope大法好 \(rope\)基本操作: #include<ext/rope> using namespace __gnu_ ...

  4. 洛谷P2464 [SDOJ2008]郁闷的小J

    洛谷P2464 [SDOJ2008]郁闷的小J 题目描述 小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他 ...

  5. 【RAC搭建报错】libcap.so.1:cannot open shared object file

    原文参考:http://blog.csdn.net/siyanyanyanyai/article/details/45306595 http://orax.blog.sohu.com/26207226 ...

  6. 实现短信超链接调起APP

    因APP推广的需求,需要给APP用户定期发送短信提醒登录使用,为了更好的用户体验在短信内容中嵌入了可以直接打开APP的超链接,下面介绍一下具体的代码实现. 编辑openApp.html文件: < ...

  7. C# 如何使用 RabbitMQ 实现消息收发

    本文是基于http://www.cnblogs.com/cheng-lei/articles/7274513.html的项目结构进行搭建的,了解之前请先阅读http://www.cnblogs.com ...

  8. 解决xampp启动mysql失败

    进入到注册表内 命令:regedit 进入到路径:计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL 修改路径为:" ...

  9. 【第七章】MySQL数据库备份-物理备份

    一.数据库备份 备份的目的: 备份: 能够防止由于机械故障以及人为误操作带来的数据丢失,例如将数据库文件保存在了其它地方. 冗余: 数据有多份冗余,但不等备份,只能防止机械故障还来的数据丢失,例如主备 ...

  10. 阿里与ShopRunner达成协议 联手在国内推出服务

    阿里巴巴集团与美国在线零售商 ShopRunner 达成协议,将帮助后者在中国大陆销售商品和履行订单交付产品. ShopRunner 首席战略官菲奥娜·迪亚斯(Fiona Dias)周三接受媒体采访时 ...