1. 本人某个android项目开发阶段一直运行良好,直到上线前夕,在某款跑着android 4.03系统的手机运行却报出一下异常,导致force close:java.lang.IllegalStateException: Can not perform this action after onSaveInstance!
首先得了解一下我那项目的一些基本情况,UI结构是TabActivity包含着5个Tabs,每个tab又是一个独立的Activity。
异常是发生在android 4.03系统上,当我在某个Tab上按Back键时,就会报出java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
从logout里发现了整个异常发生的过程:
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)
- at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399)
- at android.app.Activity.onBackPressed(Activity.java:2066)
- at android.app.Activity.onKeyUp(Activity.java:2044)
- at android.view.KeyEvent.dispatch(KeyEvent.java:2529)
- at android.app.Activity.dispatchKeyEvent(Activity.java:2274)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1855)
- at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1277)
- at android.app.Activity.dispatchKeyEvent(Activity.java:2269)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.widget.TabHost.dispatchKeyEvent(TabHost.java:297)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1855)
- at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1277)
- at android.app.Activity.dispatchKeyEvent(Activity.java:2269)
- at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803)
- at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2880)
- at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2853)
- at android.view.ViewRoot.handleMessage(ViewRoot.java:2028)
- at android.os.Handler.dispatchMessage(Handler.java:99)
- at android.os.Looper.loop(Looper.java:132)
- at android.app.ActivityThread.main(ActivityThread.java:4028)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:491)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
- at dalvik.system.NativeStart.main(Native Method)
上面的异常信息表示,我写的类不是异常的源头。根据异常信息Can not perform this action after onSaveInstanceState,可以了解到异常原因:在onSaveInstanceState行为之后,app执行某个不能响应的行为而导致异常发生。
在信息at android.app.Activity.onBackPressed(Activity.java:2066),这一句表明异常是在响应返回键响应事件的行为上发生的。我们顺藤摸瓜,考究一下在我们按下返回键时,activity会执行的响应:onKeyDown-->onBackPressed-->onPause->onStop->onDestroy。
那导火索onSaveInstanceState又是在什么时候执行的?
我们先看android API的一段原文:
- 先看Application Fundamentals上的一段话:
- Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action
- (such as pressing the BACK key)
从上面可以知道,当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候。
注意上面的双引号,何为“容易”?言下之意就是该activity还没有被销毁,而仅仅是一种可能性。
onSaveInstanceState的调用遵循一个重要原则,即当系统“未经你许可”时销毁了你的activity,则onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据。
那为什么项目里头响应onBackPressed事件时会报出上面的异常呢,还表明是after onSaveInstanceState?
原因是我Tab里面的Activity响应了onBackPressed事件,得弹出task,作为它的父容器TabActivity当然也得弹出task,TabActivity 变得“容易”被系统销毁,于是就调用onSaveInstanceState保存状态。
现在整个流程都明白了,可是,这一切都很正常啊,这个流程也很符合Activity的生命周期啊,为什么还会报异常呢?还是在最新的android 4.03上出问题,难道是说,系统不兼容?
对!
经过一番网上查阅,发现API 11 以上某些控件,包括 Fragment还有ActivityGroup,在调用saveInstanceState
存在Bug,可能是google对
saveInstanceState的实现做过修改。
直到隐藏在后面的原因,解决问题的思路就出来了:让父容器TabActivity在不调用saveInstanceState的情况下onDestroy
具体思路在tab上面的activity监听BACK键的事件,响应并拦截,再通过广播方式通知父容器TabActivity,主动销毁自己,达到原来响应onBackPressed退出App的效果。
2.做项目的时候用到fragment,在fragment快速切换的时候发生了异常:IllegalStateException: Can not perform this action after onSaveInstanceState
这个异常是由于在使用FragmentTransition的 commit方法添加一个Fragment的时候出现的
public abstract int commit ()
Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.
A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss()
for situations where it may be okay to lose the commit.
Returns
- Returns the identifier of this transaction's back stack entry, if
addToBackStack(String)
had been called. Otherwise, returns a negative number.
public abstract int commitAllowingStateLoss ()
Like commit()
but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.
这个方法与commit()一样,但是允许在activity的状态信息保存后调用。用这个方法是不安全的,有可能activity还没恢复呢就不执行commit方法了。所以这个方法只能用在ui状态转换意外的时候
再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()就行了。
3.使用Fragment遇到的异常:
1.关于Fragment(XXFragment) not attached to Activity 异常。出现该异常,是因为Fragment的还没有Attach到Activity时,调用了如getResource()等,需要上下文Content的函数。解决方法,就是等将调用的代码写在OnStart()中。网上还有几处这样的参考:http://stackoverflow.com/questions/10919240/fragment-myfragment-not-attached-to-activity 回答的主要是在调用
- getResources().getString(R.string.app_name);
- Bundle b = new Bundle();
- b.putParcelable("bitmap", bitmap2);
- imageRecognitionFragment.setArguments(b);
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState异常。分析原因:你并不需要系统保存的参数,只要你自己设置的参数能够传递过去,在另外一个Fragment里能够顺利接受就行了,现在android里提供了另外一种形式的提交方式commitAllowingStateLoss(),从名字上就能看出,这种提交是允许状态值丢失的。到此问题得到完美解决,值的传递是你自己控制的。