Fragment not attached to Activity exception

Source: Internet
Author: User
1. Fragment (XXFragment) not attached to Activity is abnormal. This exception occurs because when the Fragment has not been Attach to the Activity, the getResource () function is called and the context Content function is required. The solution is to write the called code in OnStart. There are several such references on the Internet: the http://stackoverflow.com/questions/10919240/fragment-myfragment-not-attached-to-activity answers primarily when calling getResources (). getString (R. string. app_name );

Before adding a judgment isAdded (), there is a http://stackoverflow.com/questions/6870325/android-compatibility-package-fragment-not-attached-to-activity for both sides to say this exception solution

This is an alternative solution.

2. When you use Fragment to save parameters, it may be because the parameters to be saved are large or large. In this case, the page may cause an exception. For example, code

[Java]View plaincopy

  1. Bundle B = new Bundle ();

  2. B. putParcelable ("bitmap", bitmap2 );

  3. ImageRecognitionFragment. setArguments (B );

[Java]View plaincopy

  1. Bundle B = new Bundle ();

  2. B. putParcelable ("bitmap", bitmap2 );

  3. ImageRecognitionFragment. setArguments (B );

After you set the parameters and add hide () and add (), you need to commit () to implement two Fragment redirects. In this case, the parameters must be saved by the system, however, at this time, you have implemented redirection, but the system parameters are not saved. At this time, it will be reported

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

Exception. Cause Analysis: You do not need to save the system parameters. As long as the parameters you set can be passed in and accepted in another Fragment, now, android provides another submission method in the form of commitAllowingStateLoss (). We can see from the name that this submission allows loss of status values. The problem can be solved perfectly, and the transfer of values is controlled by yourself.

Another problem is also mentioned here. bitmap can also be passed through Bundle. You can use putParacelable.



In addition:

Public final boolean isAdded ()

If the Fragment object is added to its Activity, true is returned; otherwise, false is returned.



Fragment not attached to Activity exception

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.