The issue of calling getactivity Null in Android fragment

Source: Internet
Author: User

When you use fragment, you often encounter a getactivity () null condition. For example, if I call Getactivity () in a callback for an asynchronous network request, a null pointer problem occurs. Solving this problem before is usually avoided by directly adding null pointers, but this does not really solve the problem.

Many people have been plagued by this problem, if the app is running in the background for a long time, it may appear crash when you enter the app again, and there will be overlapping fragment. If the system is out of memory, switching between screens, and the app is running in the background for a long time, theactivity may be reclaimed and rebuilt by the system, but fragment will not be recycled as the activity is recycled. All fragment created will be saved to the bundle, causing fragment to lose the corresponding activity.

Below are some of the source code of Fragmentactivity

    protected void onsaveinstancestate (Bundle outstate) {        super.onsaveinstancestate (outstate);         = mfragments.saveallstate ();         if NULL {            "android:support:fragments", p);}    }

If you click on our app from the most recently used app, the system will restore the activity that was previously recycled, and this time fragmentactivity will do fragment recovery in OnCreate.

@Overrideprotected voidonCreate (Bundle savedinstancestate) {mfragments.attachactivity ( This, Mcontainer,NULL); //Old versions of the platform didn ' t do this!        if(Getlayoutinflater (). GetFactory () = =NULL) {Getlayoutinflater (). Setfactory ( This);              } super.oncreate (Savedinstancestate); Nonconfigurationinstances NC=(nonconfigurationinstances) getlastnonconfigurationinstance (); if(NC! =NULL) {mallloadermanagers=nc.loaders; }        if(Savedinstancestate! =NULL) {parcelable P=savedinstancestate.getparcelable (Fragments_tag); Mfragments.restoreallstate (P, NC!=NULL? Nc.fragments:NULL);    } mfragments.dispatchcreate (); }

Suppose our page is called MyActivity (inherited from Fragmentactivity), where the fragment used is called Myfragment.

When this happens, the app changes as follows:

1, in the earlier mentioned cases, the system recovered the MyActivity

2. Save Myfragment Status by Onsaveinstancestate

3. The user clicks into the app again

4, because the myactivity is recycled, the system will restart the myactivity, based on the previously saved myfragment State recovery fragment

5. In myactivity code logic, the new myfragment will be created again

6, the page appears chaotic, covers two layers of fragment. If the restored myfragment uses the Getactivity () method, the null pointer exception is reported

For the above questions, consider these two workarounds:

1, do not save the state of fragment: MyActivity in the Onsaveinstancestate method, will Super.onsaveinstancestate (outstate); Let it no longer save the state of the fragment, to achieve fragment with the myactivity to destroy the purpose.

2. When rebuilding, clear the state of the saved fragment: The fragment state data in the bundle is cleared before resuming fragment. Here's how:

      if NULL )        {            =  "Android:support:fragments";            Savedinstancestate.remove (Fragments_tag);        }

The issue of calling getactivity Null in Android fragment

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.