您的位置:首页 > 其它

Fragment的checkStateLoss IllegalStateException: Can not perform this action after onSaveInstanceStat

2015-10-28 09:33 531 查看

出现该IllegalStateException的场景是这样的:

当前有一个Activity,界面上显示DialogFragment,有一个异步操作,需要去访问网络资源,然后得到网络返回的结果去dismiss当前的DialogFragment,并show其他的DialogFragment,在发起网络请求后并且结果还没返回前,按home键退出该应用,退出后一会儿网络结果返回,需要操作Fragment,抛出了IllegalStateException,原因是不能在执行onSaveInstanceState后对fragment进行commit,dismiss,show等操作。

执行onSaveInstanceState函数有以下几种情况:


1、当用户按下HOME键时。

这是显而易见的,系统不知道你按下HOME后要运行多少其他的程序,自然也不知道activity A是否会被销毁,故系统会调用onSaveInstanceState,让用户有机会保存某些非永久性的数据。以下几种情况的分析都遵循该原则

2、长按HOME键,选择运行其他的程序时。

3、按下电源按键(关闭屏幕显示)时。

4、从activity A中启动一个新的activity时。

5、屏幕方向切换时,例如从竖屏切换到横屏时。

在屏幕切换之前,系统会销毁activity A,在屏幕切换之后系统又会自动地创建activity A,所以onSaveInstanceState一定会被执行

总而言之,onSaveInstanceState的调用遵循一个重要原则,即当系统“未经你许可”时销毁了你的activity,则onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据(当然你不保存那就随便你了)。

 

至于onRestoreInstanceState方法,需要注意的是,onSaveInstanceState方法和onRestoreInstanceState方法“不一定”是成对的被调用的,onRestoreInstanceState被调用的前提是,activity A“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity A的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity A,这种情况下activity A一般不会因为内存的原因被系统销毁,故activity
A的onRestoreInstanceState方法不会被执行。

看下FragmentActivity在执行onSaveInstanceState的方法:
/**

* Save all appropriate fragment state.

*/

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

Parcelable p = mFragments.saveAllState();

if (p != null) {

outState.putParcelable(FRAGMENTS_TAG, p);

}

}看下mFragments.saveAllState方法:

<span style="font-family:SimSun;font-size:14px;">    /**
* Saves the state for all Fragments.
*/
public Parcelable saveAllState() {
return mHost.mFragmentManager.saveAllState();
}</span>
继续跟进FragmentManager的saveAllState方法:

<span style="font-family:SimSun;font-size:14px;">Parcelable saveAllState() {
// Make sure all pending operations have now been executed to get
// our state update-to-date.
execPendingActions();

if (android.os.Build.VERSION.SDK_INT >= 11) {
// As of Honeycomb, we save state after pausing.  Prior to that
// it is before pausing.  With fragments this is an issue, since
// there are many things you may do after pausing but before
// stopping that change the fragment state.  For those older
// devices, we will not at this point say that we have saved
// the state, so we will allow them to continue doing fragment
// transactions.  This retains the same semantics as Honeycomb,
// though you do have the risk of losing the very most recent state
// if the process is killed...  we'll live with that.
mStateSaved = true;
}
//其他代码忽略显示
return fms;
}</span>
可以看到在这个函数中将mStateSaved置为true,那么这个值有什么用呢?

看个具体的例子,当我们在Activity执行onSaveInstanceState之后调用fragment.show,会抛出如下异常:

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

at android.support.v4.app.FragmentManagerImpl.void checkStateLoss()(Unknown Source)

at android.support.v4.app.FragmentManagerImpl.void enqueueAction(java.lang.Runnable,boolean)(Unknown Source)

at android.support.v4.app.BackStackRecord.int commitInternal(boolean)(Unknown Source)

at android.support.v4.app.BackStackRecord.int commit()(Unknown Source)

at android.support.v4.app.DialogFragment.void show(android.support.v4.app.FragmentManager,java.lang.String)(Unknown Source)

接下来分别展示上面这几个函数的源代码:

DialogFragment.java:

<span style="font-family:SimSun;font-size:14px;">    public void show(FragmentManager manager, String tag) {
mDismissed = false;
mShownByMe = true;
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commit();
}</span>
BackStackRecord extends FragmentTransaction :

<span style="font-family:SimSun;font-size:14px;">    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);
LogWriter logw = new LogWriter(TAG);
PrintWriter pw = new PrintWriter(logw);
dump("  ", null, pw, null);
}
mCommitted = true;
if (mAddToBackStack) {
mIndex = mManager.allocBackStackIndex(this);
} else {
mIndex = -1;
}
mManager.enqueueAction(this, allowStateLoss);
return mIndex;
}</span>
FragmentManagerImpl.java:

<span style="font-family:SimSun;font-size:14px;">    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);
}
}

/**
* Adds an action to the queue of pending actions.
*
* @param action the action to add
* @param allowStateLoss whether to allow loss of state information
* @throws IllegalStateException if the activity has been destroyed
*/
public void enqueueAction(Runnable action, boolean allowStateLoss) {
if (!allowStateLoss) {
checkStateLoss();
}
synchronized (this) {
if (mDestroyed || mHost == null) {
throw new IllegalStateException("Activity has been destroyed");
}
if (mPendingActions == null) {
mPendingActions = new ArrayList<Runnable>();
}
mPendingActions.add(action);
if (mPendingActions.size() == 1) {
mHost.getHandler().removeCallbacks(mExecCommit);
mHost.getHandler().post(mExecCommit);
}
}
}</span>
看到了么,在enqueueAction的时候调用了checkStateLoss方法,在该方法中判断了前面提到的mStateSaved,发现为true则会抛出IllegalStateException: Can not perform this action after onSaveInstanceState。

dismiss和show最后都是通过fragmentTransaction.commit方法来提交的,所以这些方法在onSaveInstanceState调用都会出现一样的错误。

那刚刚看代码的时候相信大家也看到了函数中的参数:boolean allowStateLoss,发现当该参数为true的时候,enqueueAction函数中并不会检测stateLoss,那么就解决了我们的问题。

所以dismiss对应的就是dismissAllowingStateLoss,commit对应commitAllowingStateLoss,而show方法没有,所以尽量不要调用show,可以通过调用fragmentTransaction.add,然后再commitAllowingStateLoss。

那这是第一种解决办法,那还有其他什么解决办法么?

在FragmentManager中我找到了这样一个函数:

<span style="font-family:SimSun;font-size:14px;">    public void noteStateNotSaved() {
mStateSaved = false;
}</span>
这个函数做的事情很简单,就是把这个值标记为false。那有什么用呢?

刚刚我们说到FragmentActivity是在onSaveInstanceState中将该值置为true。那我们可以在onSaveInstanceState函数中再将这个值置为false啊。。。很幸运,在FragmentActivity中我发现这样一个函数:

<span style="font-family:SimSun;font-size:14px;">    /**
* Hook in to note that fragment state is no longer saved.
*/
public void onStateNotSaved() {
mFragments.noteStateNotSaved();
}</span>


所以我们可以这样在FragmentActivity中这样改:

<span style="font-family:SimSun;font-size:14px;">@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
onStateNotSaved();
}</span>
这个函数调用到最后,就是我刚刚说的那个方法。这样也可以解决。但是我觉得还是 不要用这种方法比较好,FragmentActivity在onSaveInstanceState改变该标记位应该是有用的,所以还是不要随便改变。

这里提下项目中遇到的一个需求:

当页面处于网络请求时,页面不小心锁屏或者按home键了,而网络请求还在继续,当网络请求结束回调回来时,发现页面还在后台。产品希望当页面展示在前台时再去处理网络结果(包含一些页面的展示,例如dialogfragment的显示等)。

我的做法是在activity中添加一个字段,boolean isBackground=false;

重写activity的onSaveInstanceState函数,在该函数中将isBackground=true;

在网络请求到达后,先判断if(isBackground),若为true,则将网络请求结果缓存在activity中(我的例子中会调用一个listener.onResponse(String data)函数来返回网络结果,所以我缓存了listener和data)。

原来天真的我以为重写activity的onResume函数,判断当前isBackground是否为true,若为true,将isBackground置为false,再执行listener.onResponse(data)即可。结果发现如果先锁屏再打开Activity,在onResume中处理网络请求,显示DialogFragment会有问题。还是刚刚一样的问题,IllegalStateException。然后我就在FragmentManager中找了改变mStateSaved的地方,发现了几个有意思的函数:

<span style="font-family:SimSun;font-size:14px;">    public void dispatchCreate() {
mStateSaved = false;
moveToState(Fragment.CREATED, false);
}

public void dispatchActivityCreated() {
mStateSaved = false;
moveToState(Fragment.ACTIVITY_CREATED, false);
}

public void dispatchStart() {
mStateSaved = false;
moveToState(Fragment.STARTED, false);
}

public void dispatchResume() {
mStateSaved = false;
moveToState(Fragment.RESUMED, false);
}

public void dispatchPause() {
moveToState(Fragment.STARTED, false);
}

public void dispatchStop() {
// See saveAllState() for the explanation of this.  We do this for
// all platform versions, to keep our behavior more consistent between
// them.
mStateSaved = true;

moveToState(Fragment.STOPPED, false);
}</span>
而这几个函数都是在FragmentActivity中生命周期函数中调用,看其中一个:

FragmentActivity.java:

<span style="font-family:SimSun;font-size:14px;">    /**
* Dispatch onResume() to fragments.
*/
@Override
protected void onPostResume() {
super.onPostResume();
mHandler.removeMessages(MSG_RESUME_PENDING);
onResumeFragments();
mFragments.execPendingActions();
}

/**
* This is the fragment-orientated version of {@link #onResume()} that you
* can override to perform operations in the Activity at the same point
* where its fragments are resumed.  Be sure to always call through to
* the super-class.
*/
protected void onResumeFragments() {
mFragments.dispatchResume();
}</span>
看到了么,是在onPostResume生命周期中调的,而不是onResume,所以这就解释了刚刚我项目中出现的问题。所以我只要在onPostResume中去处理存储的网络请求结果就行了。

具体了解Activity生命周期可以看该文章:http://blog.csdn.net/tounaobun/article/details/8147119,记住onPostResume是在onResume之后执行。

还有个问题:当Fragment的宿主Activity已经被关闭的之后,不能再调用dismissAllowingStateLoss或dismiss之类的函数,原因是:

/**
* Adds an action to the queue of pending actions.
*
* @param action the action to add
* @param allowStateLoss whether to allow loss of state information
* @throws IllegalStateException if the activity has been destroyed
*/
public void enqueueAction(Runnable action, boolean allowStateLoss) {
if (!allowStateLoss) {
checkStateLoss();
}
synchronized (this) {
if (mDestroyed || mHost == null) {
throw new IllegalStateException("Activity has been destroyed");
}
if (mPendingActions == null) {
mPendingActions = new ArrayList<Runnable>();
}
mPendingActions.add(action);
if (mPendingActions.size() == 1) {
mHost.getHandler().removeCallbacks(mExecCommit);
mHost.getHandler().post(mExecCommit);
}
}
}在该函数中判断了mHost和mDestroyed,所以这也是需要注意的。特别是在一些异步的回调中需要注意,例如网络回调等等。

抛出的错误如下:

Caused by: java.lang.IllegalStateException: Activity has been destroyed

at android.support.v4.app.FragmentManagerImpl.void enqueueAction(java.lang.Runnable,boolean)(Unknown Source)

at android.support.v4.app.BackStackRecord.int commitInternal(boolean)(Unknown Source)

at android.support.v4.app.BackStackRecord.int commitAllowingStateLoss()(Unknown Source)

at android.support.v4.app.DialogFragment.void dismissInternal(boolean)(Unknown Source)

at android.support.v4.app.DialogFragment.void dismissAllowingStateLoss()(Unknown Source)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: