Android Exception: Can not perform this action after Onsaveinstanc

Source: Internet
Author: User

extends:http://zhiweiofli.iteye.com/blog/1539467

One of my Android project development stage has been running well, until the eve of the launch, in a running Android 4.03 system mobile phone run but reported an exception, resulting in force Close:java.lang.IllegalStateException:Can Not perform this action after onsaveinstance!

First of all, I need to know some of my project's basic situation, the UI structure is tabactivity contains 5 tabs, each tab is a separate activity.

The exception is on the Android 4.03 system, and when I press the back key on a tab, Java.lang.IllegalStateException:Can not perform the This action after Onsaveinstancestate

The whole process of the anomaly was discovered from the logout:

Java.lang.IllegalStateException:Can not perform Thisaction 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:4028At 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)

The exception information above indicates that the class I wrote is not the source of the exception. Depending on the exception information can not perform the This action after onsaveinstancestate, you can learn about the cause of the exception: after the onsaveinstancestate behavior, the app performs an unresponsive behavior that causes an exception to occur.

At the information at android.app.Activity.onBackPressed (activity.java:2066), this sentence indicates that the exception occurred in response to the behavior of the return key response event. Let's take a look at it. When we press the Back button, the activity executes the response: Onkeydown-->onbackpressed-->onpause->onstop->ondestroy.

When was the fuse onsaveinstancestate executed?

Let's look at the original version of the Android API:

First look at application fundamentals on the passage:           Android calls Onsaveinstancestate () before the activity becomes vulnerable to being Destroyed by the system, but does isn't bother calling it when the instance are actually being destroyed by a user action
    
      (such as pressing the back key)   
    

As you can tell, when an activity becomes "easy" to be destroyed by the system, the activity's onsaveinstancestate is executed unless the activity is actively destroyed by the user, such as when the user presses the back key.

Notice the double quotes above, what is "easy"? The implication is that the activity has not been destroyed, but merely a possibility.

Onsaveinstancestate's invocation follows an important principle that when your activity is destroyed by the system "without your permission", the onsaveinstancestate is called by the system, which is the responsibility of the system, Because it has to provide an opportunity for you to save your data.

Why is it that the above exception is reported in response to the Onbackpressed event in the project, and also indicates after Onsaveinstancestate?

The reason is that the activity in my tab has responded to the onbackpressed event, the popup task, as its parent container tabactivity, of course, has to pop up task,tabactivity become "easy" to be destroyed by the system, The onsaveinstancestate is then called to save the state.

Now the whole process is understood, but this is all very normal Ah, this process is also very consistent with the life cycle of the activity ah, why also reported abnormal? Or is there a problem with the latest Android 4.03, or is it that the system is not compatible?

Right!

After some online review, we found that some of the controls in API 11, including fragment and Activitygroup, have saveInstanceState 存在Bug,可能是google对 been modified in the implementation of calling Saveinstancestate.

Until the reason behind it, the idea of solving the problem came out: let the parent container tabactivity without calling Saveinstancestate OnDestroy

Specific ideas in the tab above the activity monitoring back key events, response and interception, and then broadcast to notify the parent container tabactivity, actively destroy themselves, to achieve the original response onbackpressed quit the app effect.

Rewrite the onbackpressed of each tabview, intercept the back key event, directly in the inside to adjust the finish, and then notify the parent container tabactivity execute finish, you can avoid saveinstancestate call, try it

Android Exception: Can not perform this action after Onsaveinstanc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.