您的位置:首页 > 其它

commitallowingstateloss 和commit的区别

2015-10-15 17:56 459 查看


1、什么是FragmentTransaction?

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

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

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

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

view
sourceprint?

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的方法

view
sourceprint?

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的一个实体类

view
sourceprint?

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的代码,查看他的用法

view
sourceprint?

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,看这个方法是如何操作的。我们找到这个方法。

view
sourceprint?

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方法,看对参数进行判断后,做了什么样的处理。

view
sourceprint?

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错误
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: