1、什么是FragmentTransaction?

使用Fragment时。能够通过用户交互来运行一些动作。比方添加、移除、替换等。

全部这些改变构成一个集合,这个集合被叫做一个transaction。

能够调用FragmentTransaction中的方法来处理这个transaction,而且能够将transaction存进由activity管理的back stack中,这样用户就能够进行fragment变化的回退操作。

能够这样得到FragmentTransaction类的实例:

FragmentManager  mFragmentManager = getSupportFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();

2、commit和executePendingTransactions的差别

用add(), remove(), replace()方法,把全部须要的变化加进去,然后调用commit()方法。将这些变化应用。

在commit()方法之前,你能够调用addToBackStack(),把这个transaction增加back stack中去,这个back stack是由activity管理的。当用户按返回键时,就会回到上一个fragment的状态。

你仅仅能在activity存储它的状态(当用户要离开activity时)之前调用commit()。假设在存储状态之后调用commit()。将会抛出一个异常。

这是由于当activity再次被恢复时commit之后的状态将丢失。假设丢失也没关系。那么使用commitAllowingStateLoss()方法。

3、问什么在存储状态之后调用commit会报异常?

我们查看Android源代码发现FragmentManager和FragmentTransaction是一个虚类

那他们在activity中的实例化代码是怎样处理的呢?

首先是getSupportFragmentManager的方法

/**
* Return the FragmentManager for interacting with fragments associated
* with this activity.
*/
public FragmentManager getSupportFragmentManager() {
return mFragments;
}

查找到mFragments。

final FragmentManagerImpl mFragments = new FragmentManagerImpl();

我们发现FragmentManagerImpl是继承于FragmentManager的一个实体类

/**
* Container for fragments associated with an activity.
*/
final class FragmentManagerImpl extends FragmentManager { ........ @Override
public FragmentTransaction beginTransaction() {
return new BackStackRecord(this);
} ........ }

为了简便我们删除了一些不要的代码仅仅留下关键的方法。

通过这段代码。我们能够查看到beginTransaction方法实际返回的是一个继承于FragmentTransaction的BackStackRecord类

我们来查看BackStackRecord的代码,查看他的使用方法

/**
* @hide Entry of an operation on the fragment back stack.
*/
final class BackStackRecord extends FragmentTransaction implements
FragmentManager.BackStackEntry, Runnable { ..........
public int commit() {
return commitInternal(false);
} public int commitAllowingStateLoss() {
return commitInternal(true);
} int commitInternal(boolean allowStateLoss) {
if (mCommitted) throw new IllegalStateException("commit already called");
if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Commit: " + this);
mCommitted = true;
if (mAddToBackStack) {
mIndex = mManager.allocBackStackIndex(this);
} else {
mIndex = -1;
}
mManager.enqueueAction(this, allowStateLoss);
return mIndex;
}
.......... }

绕了大半天,最终找到commit方法和commitAllowingStateLoss方法,他们都同一时候调用了commitInternal方法,仅仅是传的參数略有不同。一个是true。一个是false。我们发如今运行这种方法之前会首先对mCommitted进行推断,依据代码语义我们能够知道mCommitted就是是否已经commit的意思

最后,commitInternal调用了mManager.enqueueAction的方法。

让我们回到FragmentManager,看这种方法是怎样操作的。

我们找到这种方法。

/**
* @hide Entry of an operation on the fragment back stack.
*/
final class BackStackRecord extends FragmentTransaction implements
FragmentManager.BackStackEntry, Runnable { ..........
public int commit() {
return commitInternal(false);
} public int commitAllowingStateLoss() {
return commitInternal(true);
} int commitInternal(boolean allowStateLoss) {
if (mCommitted) throw new IllegalStateException("commit already called");
if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Commit: " + this);
mCommitted = true;
if (mAddToBackStack) {
mIndex = mManager.allocBackStackIndex(this);
} else {
mIndex = -1;
}
mManager.enqueueAction(this, allowStateLoss);
return mIndex;
}
.......... }

经分析后,我们能够发现。此方法在对 commit和commitAllowingStateLoss的传參进行推断后。将任务扔进activity的线程队列中。那这个两个方法差别就在传參推断后的处理方法checkStateLoss,那接下来,让我们查看一下checkStateLoss方法。看对參数进行推断后,做了什么样的处理。

private void checkStateLoss() {
if (mStateSaved) {
throw new IllegalStateException(
"Can not perform this action after onSaveInstanceState");
}
if (mNoTransactionsBecause != null) {
throw new IllegalStateException(
"Can not perform this action inside of " + mNoTransactionsBecause);
}
}

ok,到这里。真相总算大明。当使用commit方法时,系统将进行状态推断,假设状态(mStateSaved)已经保存,将发生"Can not perform this action after onSaveInstanceState"错误。

假设mNoTransactionsBecause已经存在,将发生"Can not perform this action inside of " + mNoTransactionsBecause错误。

FragmentTransaction的commit和commitAllowingStateLoss的差别的更多相关文章

  1. Fragment回退栈&commit()和commitAllowingStateLoss()

    Activity切换时是通过栈的形式,不断压栈出栈,在Fragment的时候,如果你不是手动开启回退栈,它是直接销毁再重建,但如果将Fragment任务添加到回退栈,情况就会不一样了,它就有了类似Ac ...

  2. FragmentTransaction的commit的异步操作

    FragmentTransaction是异步的,commit()仅是相当于把操作加入到FragmentManager的队列,然后FragmentManager会在某一个时刻来执行,并不是立即执行.所以 ...

  3. 从源码看commit和commitAllowingStateLoss方法区别

    Fragment介绍 在很久以前,也就是我刚开始写Android时(大约在2012年的冬天--),那时候如果要实现像下面微信一样的Tab切换页面,需要继承TabActivity,然后使用TabHost ...

  4. commit(), commitNow()和commitAllowingStateLoss()

    关于FragmentTransaction的各种提交方法: commit(),commitAllowingStateLoss(),commitNow()和commitNowAllowingStateL ...

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

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

  6. Fragment Transactions和Activity状态丢失

    本文由 伯乐在线 - 独孤昊天 翻译.未经许可,禁止转载!英文出处:androiddesignpatterns.欢迎加入翻译组. 下面的堆栈跟踪和异常代码,自从Honeycomb的初始发行版本就一直使 ...

  7. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

  8. Android Weekly Notes Issue #220

    Android Weekly Issue #220 August 28th, 2016 Android Weekly Issue #220 ARTICLES & TUTORIALS Manag ...

  9. Fragment提交transaction导致state loss异常

    下面自从Honeycomb发布后,下面栈跟踪信息和异常信息已经困扰了StackOverFlow很久了. java.lang.IllegalStateException: Can not perform ...

随机推荐

  1. http --- 从输入URL到页面加载的过程发生了什么?

    可以分为这几个大的过程: DNS解析 TCP连接 客户端发送HTTP请求 服务器处理请求并返回HTTP报文 浏览器解析渲染页面 结束 其中(1)DNS解析可以理解为主寻找这个IP地址的过程,其中如果找 ...

  2. 使用CSS3制作网站常用的小三角形

    现在在前端开发中,经常会看到一些小三角形,如一些导航的下拉菜单,还有一些聊天信息的气泡模式,很多时候我们都是通过切图片的方法来制作,今天零度给大家分享一个完全通过css3实现的小三角效果. 先上htm ...

  3. PHP万能的连接数据库

    <?php class DB{ const HOST='127.0.0.1'; const USER='root'; const PASS='root'; const DATA='mooc'; ...

  4. Hello World基于.net framework中CLR的执行

    static void Main(string[] args) { Console.WriteLine("Hello,World!"); Console.WriteLine(&qu ...

  5. input的选中与否以及将input的value追加到一个数组里

    html布局 <div class="mask"> //每一个弹层都有一个隐藏的input <label> <input hidden="& ...

  6. ArcGIS api for javascript——地图配置-定制平移动画

    描述 本例展示了当用户点击平移按钮时如何定制地图的动画.panDuration和panRate是Dojo动画属性,可以分别确定动画的duration和帧刷新的rate.这些属性的单位都是毫秒,panD ...

  7. 《R实战》读书笔记二

    第一章 R简单介绍 本章概要 1安装R 2理解R语言 3执行R程序 本章所介绍的内容概括例如以下. 一个典型的数据分析步骤如图1所看到的. 图1:典型数据分析步骤 简而言之,现今的数据分析要求我们从多 ...

  8. stl之set集合容器应用基础

    set集合容器使用一种称为红黑树(Red-Black Tree) 的平衡二叉检索树的数据结构,来组织泛化的元素数据.每一个节点包括一个取值红色或黑色的颜色域.以利于进行树的平衡处理.作为节点键值的元素 ...

  9. Delphi的参数修饰const/var/output 与C++的对应关系

    delphi的const/input和默认的没有修饰, C++都是一样的 delphi的var,对应C++那边是指针,  调用方需要管理内存(负责分配内存及销毁) delphi的output , 对应 ...

  10. VC双缓冲画图技术介绍

    双缓冲画图,它是一种主要的图形图像画图技术.首先,它在内存中创建一个与屏幕画图区域一致的对象,然后将图形绘制到内存中的这个对象上,最后把这个对象上的图形数据一次性地拷贝并显示到屏幕上. 这样的技术能够 ...