程序员人生 网站导航

Android - 解决onSaveInstanceState的Bug

栏目:综合技术时间:2015-02-02 08:43:56

解决onSaveInstanceState的Bug


本文地址:http://blog.csdn.net/caroline_wendy


Bug:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(Unknown Source) at android.support.v4.app.FragmentManagerImpl.enqueueAction(Unknown Source) at android.support.v4.app.BackStackRecord.commitInternal(Unknown Source) at android.support.v4.app.BackStackRecord.commit(Unknown Source) at android.support.v4.app.FragmentTabHost.onAttachedToWindow(Unknown Source) at android.view.View.dispatchAttachedToWindow(View.java:12618) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2467) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2467) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2467) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2467) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2467) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1262) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1045) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5908) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:780) at android.view.Choreographer.doCallbacks(Choreographer.java:593) at android.view.Choreographer.doFrame(Choreographer.java:562) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:766) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5336) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) at dalvik.system.NativeStart.main(Native Method)


参考:
http://stackoverflow.com/questions/7575921/illegalstateexception-can-not-perform-this-action-after-onsaveinstancestate-h
http://stackoverflow.com/questions/14278763/call-onsaveinstancestate-without-calling-super-onsaveinstancestateoutstate

解决:
重载onSaveInstanceState()中,不调用super()方法,由于调用super方法,在API level大于11时,可能致使崩溃
@Override protected void onSaveInstanceState(Bundle outState) { //No call for super(). Bug on API Level > 11. // super.onSaveInstanceState(outState); }

onSaveInstanceState()方法的作用: 
onSaveInstanceState的调用遵守原则,即当系统“未经用户许可”时烧毁activity,则onSaveInstanceState会被系统调用。
这是系统的责任,由于它必须要提供1个机会让用户保存数据。

super.onSaveInstanceState(outState):
活动本身会在bundle中存入1些值,例如fragments的状态,如果没有调用super方法,这些值不会被保存,但也不会使程序崩溃。








------分隔线----------------------------
------分隔线----------------------------

最新技术推荐