FragmentTransaction的commit和commitAllowingStateLoss的区别

原文传送门

[+]

1、什么是FragmentTransaction?

使用Fragment时,可以通过用户交互来执行一些动作,比如增加、移除、替换等。

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

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

可以这样得到FragmentTransaction类的实例:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. FragmentManager  mFragmentManager = getSupportFragmentManager();  
  2. 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的方法
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * Return the FragmentManager for interacting with fragments associated 
  3.      * with this activity. 
  4.      */  
  5.     public FragmentManager getSupportFragmentManager() {  
  6.         return mFragments;  
  7.     }  


查找到mFragments。
final FragmentManagerImpl mFragments = new FragmentManagerImpl();
我们发现FragmentManagerImpl是继承于FragmentManager的一个实体类
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Container for fragments associated with an activity. 
  3.  */  
  4. final class FragmentManagerImpl extends FragmentManager {  
  5.       
  6.     ........  
  7.   
  8.   
  9.     @Override  
  10.     public FragmentTransaction beginTransaction() {  
  11.         return new BackStackRecord(this);  
  12.     }  
  13.   
  14.   
  15.     ........  
  16.   
  17.   
  18.     }  


为了简便我们删除了一些不要的代码只留下关键的方法。
通过这段代码,我们可以查看到beginTransaction方法实际返回的是一个继承于FragmentTransaction的BackStackRecord类
我们来查看BackStackRecord的代码,查看他的用法
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * @hide Entry of an operation on the fragment back stack. 
  3.  */  
  4. final class BackStackRecord extends FragmentTransaction implements  
  5.         FragmentManager.BackStackEntry, Runnable {  
  6.   
  7.   
  8.     ..........  
  9.     public int commit() {  
  10.         return commitInternal(false);  
  11.     }  
  12.   
  13.   
  14.     public int commitAllowingStateLoss() {  
  15.         return commitInternal(true);  
  16.     }  
  17.   
  18.   
  19.     int commitInternal(boolean allowStateLoss) {  
  20.         if (mCommitted) throw new IllegalStateException("commit already called");  
  21.         if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Commit: " + this);  
  22.         mCommitted = true;  
  23.         if (mAddToBackStack) {  
  24.             mIndex = mManager.allocBackStackIndex(this);  
  25.         } else {  
  26.             mIndex = -1;  
  27.         }  
  28.         mManager.enqueueAction(this, allowStateLoss);  
  29.         return mIndex;  
  30.     }  
  31.     ..........  
  32.   
  33.   
  34. }  


绕了大半天,终于找到commit方法和commitAllowingStateLoss方法,他们都同时调用了commitInternal方法,只是传的参数略有不同,一个是true,一个是false。我们发现在执行这个方法之前会首先对mCommitted进行判断,根据代码语义我们可以知道mCommitted就是是否已经commit的意思
最后,commitInternal调用了mManager.enqueueAction的方法。让我们回到FragmentManager,看这个方法是如何操作的。我们找到这个方法。
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * @hide Entry of an operation on the fragment back stack. 
  3.  */  
  4. final class BackStackRecord extends FragmentTransaction implements  
  5.         FragmentManager.BackStackEntry, Runnable {  
  6.   
  7.   
  8.     ..........  
  9.     public int commit() {  
  10.         return commitInternal(false);  
  11.     }  
  12.   
  13.   
  14.     public int commitAllowingStateLoss() {  
  15.         return commitInternal(true);  
  16.     }  
  17.   
  18.   
  19.     int commitInternal(boolean allowStateLoss) {  
  20.         if (mCommitted) throw new IllegalStateException("commit already called");  
  21.         if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Commit: " + this);  
  22.         mCommitted = true;  
  23.         if (mAddToBackStack) {  
  24.             mIndex = mManager.allocBackStackIndex(this);  
  25.         } else {  
  26.             mIndex = -1;  
  27.         }  
  28.         mManager.enqueueAction(this, allowStateLoss);  
  29.         return mIndex;  
  30.     }  
  31.     ..........  
  32.   
  33.   
  34. }  


经分析后,我们可以发现,此方法在对 commit和commitAllowingStateLoss的传参进行判断后,将任务扔进activity的线程队列中。那这个两个方法区别就在传参判断后的处理方法checkStateLoss,那接下来,让我们查看一下checkStateLoss方法,看对参数进行判断后,做了什么样的处理。
[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. private void checkStateLoss() {  
  2.         if (mStateSaved) {  
  3.             throw new IllegalStateException(  
  4.                     "Can not perform this action after onSaveInstanceState");  
  5.         }  
  6.         if (mNoTransactionsBecause != null) {  
  7.             throw new IllegalStateException(  
  8.                     "Can not perform this action inside of " + mNoTransactionsBecause);  
  9.         }  
  10.     }  


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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值