为了账号安全,请及时绑定邮箱和手机立即绑定

FragmentTransaction的commit和commitAllowingStateLoss的区别

标签:
Android

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错误。

原文链接:http://www.apkbus.com/blog-813041-62896.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消