Call getActivity () in Fragment to report null. activity calls fragment.

Source: Internet
Author: User

Call getActivity () in Fragment to report null. activity calls fragment.

Check umeng's error log and find that NullPointerException occurs again. Then, check the code and find that only the context is available, however, this situation has already occurred before, so I uploaded the context from the Activity when I created the Fragment object. I tested it multiple times and there is no problem, after finding information on the Internet, I found many cases where getActivity () is empty. I found that I ignored the following problem: If the app runs in the background for a long time, crash may occur when you enter the app again. The Activity may be recycled by the system and rebuilt. However, Fragment will not be recycled as the Activity is recycled, and the created Fragment will be saved to the Bundle, this causes Fragment to lose the Activity.

To solve this problem, I think of a solution: when the Activity is destroyed, the bound Fragmetn is destroyed at the same time, the onSaveInstanceState method is rewritten in the Activity, and super is commented out. onSaveInstanceState (outState), so that the Activity does not save the Fragment status.

Related Article

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.