============2
Fix Illegalstateexception:can not perform this action after Onsaveinstancestate
This error occurs when using fragment today Illegalstateexception:can not perform this action after Onsaveinstancestate:
- E/androidruntime (12747): caused By:java.lang.IllegalStateException:Can not perform this action after Onsaveinstancestate
- At Android.support.v4.app.FragmentManagerImpl.checkStateLoss (Fragmentmanager.java:1314)
- At Android.support.v4.app.FragmentManagerImpl.enqueueAction (Fragmentmanager.java:1325)
is in the use of Fragmenttransition's Commit method to add a fragment, and later found on the official website of the relevant
Description: Http://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss ()
public abstract int commitallowingstateloss () Added in API level One
Like but allows, the commit to being executed after an activity's is commit()
saved. This is dangerous
Because the commit can lost if the activity needs to later is restored from it state, so this should
Only being used for cases where it's okay for the UI state to change unexpectedly on the user.
The general meaning is that the commit method I'm using is called after the activity's Onsaveinstancestate (), which makes an error, because onsaveinstancestate
The method is called before the activity is about to be destroyed to save the activity data, and if you save the play state and then add fragment to it, the error occurs. The solution is
is to replace the commit () method with Commitallowingstateloss (), and the effect is the same.
========1
treatment of java.lang.IllegalStateException:Fragment already added anomalies
This exception is caused when a quick double-click calls the Fragmenttransaction.add () method to add Fragmenta, and Fragmenta is not generated separately each time.
The Fragmenttransaction.add () method is called internally by Dialogfragment.show (), so this exception may also occur when calling the Dialogfragment.show () method.
In the Add () method, first Judge fragmenta.isadded (), such as the downgrade can avoid the exception:
if (!fragmenta.isadded ()) {
Fragmentmanager manager = ((fragmentactivity) context). Getsupportfragmentmanager ();
Fragmenttransaction ft = manager.begintransaction ();
Ft.add (Fragmenta, "fragment_name");
Ft.commit ();
}
Fragment's bug??