Activity backstage run some time back crash problem analysis and solution

Source: Internet
Author: User

It took a lot of time to find the cause and solve the problem when I was doing the project recently. I would like to record this hole I stepped on, I hope other friends encounter this problem do not transfer to this hole.

Problem Description:

1. Background: The activity of an interface in my app is inherited fragmentactivity, because this interface contains two fragment. Here I call Fragmenta and FRAGMENTB. There is a refresh button in activity to refresh the Viewpager current fragment content. After clicking on the activity's Refresh button, the Refresh button requires a simple rotation animation, and when the refresh in fragment is finished, the refresh animation of the activity End Refresh button is notified using Getactivity. The above is my business scenario, saying that the simple point is that fragment needs to communicate with the activity it is affiliated with.

2. Problem: When the application runs to the activity, press the home key to place the application in the background and go to other apps. After some time, go back to the application, or the previous activity. At this time I want to refresh the contents of fragment inside, click on the Activity Interface Refresh button, the results of the program crash.

Problem Analysis:

When you first encounter this problem, look at the crash log and find that it is a null pointer exception. Because this kind of scene is not many, so simply add the non-null judgment to not care about this problem. To the back of a test machine, the configuration is not very good (only 512M running memory), the result of this problem occurs frequently, began to arouse my attention. Because the experience is not very sufficient, this problem does not know how to reproduce, so it is difficult to find the root cause of the problem. I finally found a post on the Internet and I met a friend of the same problem, only to know the cause of the problem.

After the activity was switched to the background, the activity was reclaimed by the system due to insufficient memory, and the activity was re-instantiated after a period of time to return to the application. When the activity is destroyed by the system, the fragment attached to the activity is not destroyed and the fragment state is saved in the activity's onsaveinstancestate, so the activity is recreated, However, Fragmenta and FRAGMENTB are still in the past, and Fragmenta and FRAGMENTB's activity has been reclaimed by the system, this time the call to Getactivity returned NULL, only to cause the above problem.

Let's look at the Onsaveinstancestate method in Fragmentactivity source code:

1 protected voidonsaveinstancestate (Bundle outstate)2    {3      Super. Onsaveinstancestate (outstate);4Parcelable p =mfragments.saveallstate ();5      if(P! =NULL) {6Outstate.putparcelable ("Android:support:fragments", p);7     }8}

From the above source can be seen, fragmentactivity indeed in the Onsaveinstancestate method inside will fragment state saved.

Problem solving:

Know the cause of the problem, it is good to run. The solution is actually very simple, we just let fragmentactivity be collected by the system, do not save fragment state, that is, in fragmentactivity to rewrite the Onsaveinstancestate method, and comment out Super.onsaveinstancestate (outstate) on the line.

1     @Override 2     protected void onsaveinstancestate (Bundle outstate) {3//        super.onsaveinstancestate ( outstate); 4     }

Summarize:

1, the program problems, we must first find out the cause of this problem, the remedy can fundamentally solve the problem.

2, for the activity is caused by the system recovery problems, you can use the toggle screen to simulate the scene.

Finally thank the friend who wrote http://my.oschina.net/u/1011854/blog/469138 this post.

Activity background run some time back crash problem analysis and resolution

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.