Fargment调用commit():方法出错,报出以下异常

有时你会发现,Fargment调用commit():方法出错,报出以下异常


只要将调用commit()的地方换成commitAllowingState();


我们查看Android源码发现FragmentManager和FragmentTransaction是一个虚类
那他们在activity中的实例化代码是如何处理的呢?
首先是getSupportFragmentManager的方法

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的一个实体类
01. /**
02. * Container for fragments associated with an activity.
03. */
04. final class FragmentManagerImpl extends FragmentManager {
05.  
06. ........
07.  
08.  
09. @Override
10. public FragmentTransaction beginTransaction() {
11. return new BackStackRecord(this);
12. }
13.  
14.  
15. ........
16.  
17.  
18. }


为了简便我们删除了一些不要的代码只留下关键的方法。
通过这段代码,我们可以查看到beginTransaction方法实际返回的是一个继承于FragmentTransaction的BackStackRecord类
我们来查看BackStackRecord的代码,查看他的用法
01. /**
02. * @hide Entry of an operation on the fragment back stack.
03. */
04. final class BackStackRecord extends FragmentTransaction implements
05. FragmentManager.BackStackEntry, Runnable {
06.  
07.  
08. ..........
09. 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,看这个方法是如何操作的。我们找到这个方法。
01. /**
02. * @hide Entry of an operation on the fragment back stack.
03. */
04. final class BackStackRecord extends FragmentTransaction implements
05. FragmentManager.BackStackEntry, Runnable {
06.  
07.  
08. ..........
09. 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方法,看对参数进行判断后,做了什么样的处理。
01. private void checkStateLoss() {
02. if (mStateSaved) {
03. throw new IllegalStateException(
04. "Can not perform this action after onSaveInstanceState");
05. }
06. if (mNoTransactionsBecause != null) {
07. throw new IllegalStateException(
08. "Can not perform this action inside of " + mNoTransactionsBecause);
09. }
10. }


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




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值