FragmentTransaction的commit和commitAllowingStateLoss的差别
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的差别的更多相关文章
- Fragment回退栈&commit()和commitAllowingStateLoss()
Activity切换时是通过栈的形式,不断压栈出栈,在Fragment的时候,如果你不是手动开启回退栈,它是直接销毁再重建,但如果将Fragment任务添加到回退栈,情况就会不一样了,它就有了类似Ac ...
- FragmentTransaction的commit的异步操作
FragmentTransaction是异步的,commit()仅是相当于把操作加入到FragmentManager的队列,然后FragmentManager会在某一个时刻来执行,并不是立即执行.所以 ...
- 从源码看commit和commitAllowingStateLoss方法区别
Fragment介绍 在很久以前,也就是我刚开始写Android时(大约在2012年的冬天--),那时候如果要实现像下面微信一样的Tab切换页面,需要继承TabActivity,然后使用TabHost ...
- commit(), commitNow()和commitAllowingStateLoss()
关于FragmentTransaction的各种提交方法: commit(),commitAllowingStateLoss(),commitNow()和commitNowAllowingStateL ...
- 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)
即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...
- Fragment Transactions和Activity状态丢失
本文由 伯乐在线 - 独孤昊天 翻译.未经许可,禁止转载!英文出处:androiddesignpatterns.欢迎加入翻译组. 下面的堆栈跟踪和异常代码,自从Honeycomb的初始发行版本就一直使 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Android Weekly Notes Issue #220
Android Weekly Issue #220 August 28th, 2016 Android Weekly Issue #220 ARTICLES & TUTORIALS Manag ...
- Fragment提交transaction导致state loss异常
下面自从Honeycomb发布后,下面栈跟踪信息和异常信息已经困扰了StackOverFlow很久了. java.lang.IllegalStateException: Can not perform ...
随机推荐
- ASP.NET Application and Page Life Cycle
ASP.NET Application Life Cycle Overview for IIS 7.0 https://msdn.microsoft.com/en-us/library/bb47025 ...
- 智课雅思短语---四、Exploit to the full one’s favorableconditions and avoid unfavorable ones
智课雅思短语---四.Exploit to the full one’s favorableconditions and avoid unfavorable ones 一.总结 一句话总结:扬长避短 ...
- SQL Server单表已700w+将普通表转换成分区表1
最近项目中,某个表数据量爆发时增长,单表已700w+,读写性能急剧下降,所以考虑加入分区表以解燃眉之急,后续还是要分表分库,当然这是后话.下面简要说一下将普通表转为分区表的步骤. 一.创建文件组 ...
- channels2.X 学习笔记
- No module named 'asgiref.sync' 报错解决: # 报错原因: """ django版本过低, 卸载最新版本的 channels 使用2.x ...
- Codeforces434D 网络流
思路: 题意:有n<=50个点,每个点有xi有[li, ri]种取值,-100 <= li <= ri <= 100,并且给定m<=100条边,每条边为u,v,d表示xu ...
- PostgreSQL Replication之第八章 与pgbouncer一起工作(2)
8.2 安装pgbouncer 在我们深入细节之前,我们将看看如何安装pgbouncer.正如PostgreSQL一样,您可以采取两种途径.您可以安装二进制包或者直接从源代码编译.在我们的例子中,我们 ...
- c# static 常量
1 关键字 static 修饰 类 字段 属性 方法 ,标记static的就不用创建类的实例调用了,直接通过类名点出来 2 用于变量前,表示每次重新使用该变量所在的方法,类或者自定义的类时,变量的值 ...
- BZOJ 3626 LCA(离线+树链剖分+差分)
显然,暴力求解的复杂度是无法承受的. 考虑这样的一种暴力,我们把 z 到根上的点全部打标记,对于 l 到 r 之间的点,向上搜索到第一个有标记的点求出它的深度统计答案.观察到,深度其实就是上面有几个已 ...
- POJ2104 K-th Number(线段树,二分,vector)
题意 不带修改区间第k小.(n<=100000) 题解 建立线段数和vector数组(vector为当前区间排列之后的序列)(归并) 然后对于每一个询问二分答案. 问题就转化为区间有多少数小于等 ...
- Python爬虫基础示例
使用pip安装相关依赖: pip install requests pip install bs4 安装成功提示:Successfully installed *... 爬取中国天气网数据示例代码: ...