Java.lang.IllegalStateException:Can not perform this action after onsaveinstancestate

Source: Internet
Author: User

1. Scene:

In the development process encountered such a need, on the home page click the button to enter another activity (readactivity), click on a button in the readactivity to return to the home page and specify the page to select a specific tab. Home is Fragmenttabhost + Fragment implementation. The idea is to readactivity back through Startactivityforresult and Setresult () and Requestcode as the sign, because there are other requestcode. Again through

Fragmenttabhost's Setcurrenttab (int index) method Toggles tab, and I put the method in Startactivityforresult (), This issue occurs Java.lang.IllegalStateException:Can not perform this action after onsaveinstancestate.

2. Why?

Consult the source code, enter into the source code of Fragmentmanager to find such an explanation:

/*** Start A series of edit operations on the fragments associated with * this fragmentmanager.  * * <p>note:a Fragment transaction can only is created/committed prior * to an activity saving it state. If you try to commit a transaction * after {@linkfragmentactivity#onsaveinstancestate fragmentactivity.onsaveinstancestate ()} * (and prior to a following {@linkFragmentactivity#onstart Fragmentactivity.onstart} * or {@linkfragmentactivity#onresume Fragmentactivity.onresume ()}, you'll get an error. * This is because the framework takes care of saving your current fragments * in the state, and if changes are made AF Ter The state was saved then they * would be lost.</p>*/     Public AbstractFragmenttransaction BeginTransaction ();

The main idea is that a fragment transaction can only be created (created) or committed (committed ) when the dependent activity is in the saved state, if the Onsaveinstancestate () then commits the transaction, causing the problem to occur-the can not perform the this action after onsaveinstancestate.

3. Timing of Occurrence:

 Let's look at this piece of code in Fragmentmanager (just show the relevant code): (static Final Boolean honeycomb = Android.os.Build.VERSION. Sdk_int >= ; )

parcelable saveallstate () {//Make sure all pending operations has now been executed to get//Our state update-to-date.execpendingactions (); if(Honeycomb) {//As of honeycomb, we save state after pausing. Prior to that//it is before pausing. With fragments a issue, since//There is many things you could do after pausing but before//stopping the fragment state. For those older//devices, we 'll not be at the this point say we have saved//the state, so we'll allow them to continue doing fragment//transactions. This retains the same semantics as Honeycomb,//Though do has the risk of losing the very most recent state//If the process is killed ... we ll live with that.mstatesaved =true; }}

Just look at the comment in if: honeycomb-a system greater than or equal to the Android3.0 version, the activity saves its state after an invisible state (pausing), and the mstatesaved state is true. For 3.0 earlier devices, the fragment transaction is allowed to execute after an invisible state, so the save state is indeterminate, but the state is definitely saved before the stop.

  mstatesaved (Boolean) flag whether the state is saved:

 private  void   Checkstateloss () { if   (mstatesaved) { throw  new IllegalStateException ( "Can not perform this action after OnSave        Instancestate ");  if  (mnotransactionsbecause! = null throw  new   IllegalStateException ( "Can not perform this action ins        IDE of "+ Mnotransactionsbecause); }    }
 static final boolean honeycomb = Android.os.Build.VERSION. sdk_int >= 11; 

4. Idea finishing:

Jumping from the home page to readactivity,mainactivity (home page) is no longer visible and saves its state, and the commit operation cannot be performed on fragment at this time. The fragmenttabhost setcurrenttab (int index)
calls the Commit method, causing the problem to occur. setcurrenttab () , invokeontabchangelistener () ontabchanged (), ft.commit (); Glance!!!
1  Public void Setcurrenttab(intindex) {2         if(Index < 0 | | | Index >=mtabspecs.size ()) {3             return;4         }5 6         if(Index = =Mcurrenttab) {7             return;8         }9 Ten         //notify old tab content One         if(Mcurrenttab! =-1) { A Mtabspecs.get (Mcurrenttab). mcontentstrategy.tabclosed (); -         } -  theMcurrenttab =index; -         FinalTabhost.tabspec spec =Mtabspecs.get (index); -  -         //Call the tab widget ' s Focuscurrenttab (), instead of just +         //Selecting the tab. - Mtabwidget.focuscurrenttab (mcurrenttab); +  A         //Tab Content atMcurrentview =Spec.mContentStrategy.getContentView (); -  -         if(mcurrentview.getparent () = =NULL) { - mtabcontent - . AddView ( - Mcurrentview, in                             NewViewgroup.layoutparams ( - ViewGroup.LayoutParams.MATCH_PARENT, to ViewGroup.LayoutParams.MATCH_PARENT)); +         } -  the         if(!Mtabwidget.hasfocus ()) { *             //If the tab widget didn ' t take focus (likely because we ' re in touch mode) $             //give the current tab content view a shotPanax Notoginseng Mcurrentview.requestfocus (); -         } the  +         //Mtabcontent.requestfocus (view.focus_forward); A Invokeontabchangelistener (); the     } +  -     /** $ * Register A callback to is invoked when the selected state of any of the items $ * In this list changes -      * @paramL - * The callback that would run the      */ -      Public voidSetontabchangedlistener (Ontabchangelistener l) {WuyiMontabchangelistener =l; the     } -  Wu     Private void Invokeontabchangelistener() { -         if(Montabchangelistener! =NULL) { About Montabchangelistener. ontabchanged (Getcurrenttabtag ()); $         } -     } -  - @Override A      Public void ontabchanged(String tabId) { +         if(mattached) { theFragmenttransaction ft = dotabchanged (tabId,NULL); -             if(ft! =NULL) { $ ft.commit(); the             } the         } the         if(Montabchangelistener! =NULL) { the montabchangelistener.ontabchanged (tabId); -         } in}

5. Resolution:

  Knowing the problem is the point, as long as the Setcurrenttab () method is executed before the activity is saved, the key is to execute the commit method. Either before the current unsaved state, or when the activity is again visible and not onpause before the problem, OH.

Java.lang.IllegalStateException:Can not perform this action after onsaveinstancestate

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.