Fragment's bug??

Source: Internet
Author: User

============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:

    1. E/androidruntime (12747): caused By:java.lang.IllegalStateException:Can not perform this action after Onsaveinstancestate
    2. At Android.support.v4.app.FragmentManagerImpl.checkStateLoss (Fragmentmanager.java:1314)
    3. 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??

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.