After the app is switched back and forth in the resolved project, you are prompted to "stopped running"

Source: Internet
Author: User
Tags static class

After debugging, I found that it was applied when we called a global variable, and the global variable was empty, but we had a setting and we had a value before we switched it.

Cause Analysis:

Finally on the Internet to find a bit, originally in memory less time , back and forth when the GC will go to release some memory , the global variable will be reset to the default value,


Methods of Simulation:

1: First declare a static global variable class:

Package Com.example.kodulf.crashdemo;

/**
 * Created by Kodulf on 2017/5/12.
 */Public

class Staticclass {public

    static String staticstring;
    public static int[] Intarray;
}

2: Create a mainactivity

Package Com.example.kodulf.crashdemo;

Import android.content.Intent;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;

public class Mainactivity extends Appcompatactivity {

    @Override
    protected void OnCreate (Bundle Savedinstancestate) {
        super.oncreate (savedinstancestate);
        Setcontentview (r.layout.activity_main);


        staticclass.staticstring = "Hello";
        Initialize the array inside the static class, giving a very large memory bright
        Staticclass.intarray = new int[1024*1024*25];
        Start a second activity
        Intent Intent = new Intent (this,secondactivity.class);
        StartActivity (intent);

    }
}

3: Create Secondactivity

Package Com.example.kodulf.crashdemo;

Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.util.Log;

public class Secondactivity extends Appcompatactivity {

    @Override
    protected void OnCreate (Bundle Savedinstancestate) {
        super.oncreate (savedinstancestate);
        Setcontentview (R.layout.activity_second);

        LOG.D ("Kodulf", "+staticclass.staticstring");
        Below here
        //In the current display is secondactivity switch to the background, and then switch back, in the case of low memory, will be error
        //In the current display is secondactivity when the switch to the background, There is a way to let him directly error, is through some memory management software, in the current display is secondactivity switch to the background, click on the memory cleanup, and then switch back will be error
        LOG.D ("Kodulf", "" "+ StaticClass.intArray.length);

    }

}


4: The most direct simulation method:

Starting the app, the default is to initialize the static int array at mainactivity and then start automatically secondactivity

This time the app is switched to the background

Find a memory management tool, such as a 360 software assistant, for memory cleanup,

If you switch the app back, you'll get a direct error.



Workaround:

Use constants as much as possible for global variables, and if you want to involve modified global variables, it's best not to eat directly

1: You can use Sharepreference to save and then.

2: Or save this global variable at onsaveinstancestate, and then re-assign the global variable when onrestoreinstancestate.

Package Com.example.kodulf.crashdemo;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;

Import Android.util.Log; public class Secondactivity extends appcompatactivity {@Override protected void onCreate (Bundle savedinstancesta
        TE) {super.oncreate (savedinstancestate);

        Setcontentview (R.layout.activity_second);
        LOG.D ("Kodulf", "+staticclass.staticstring"); Below here//in the current display is secondactivity switch to the background, and then switch back, in the case of low memory, will be error//In the current display is secondactivity when the switch to the background, there is a direct error to his method, is through some memory management software, in the current display is secondactivity switch to the background, click on the memory cleanup, and then switch back will be error log.d ("Kodulf", "" +staticclass.intarray.length

    );

        } @Override protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate);

    Outstate.putserializable ("Intarray", Staticclass.intarray); } @Override protected void Onrestoreinstancestate (Bundle savedinstancestate) {Super.onrestoreinstancesta Te (savedinsTancestate);
        
    Staticclass.intarray = (int[]) savedinstancestate.getserializable ("Intarray");
 }
}




+++++++++++++++++++++++++++++++++++++++++

Reference: http://blog.csdn.net/hs_73/article/details/50911133

Late testing, found that there is an occasional phenomenon, when the application is switched to the background for a period of time to switch back to the time will be inexplicable crash, and in the log error message, Cloud Test and the Friends of the league error List are not found error log output. It's really tricky, but it's an app bug that can't be overlooked, but it's not easy to reproduce. Later search data analysis, may be related to these several reasons:

1. The difference between getcontext (), getactivity (), Activity.this, Getapplicationcontext is not properly understood, and the use of chaos leads to:

Context: Contextual (refers to the running environment on which this application is run), Context Category: ①.activity: Specific context; Activity.this's context belongs to the activity, and the activity destroys it to destroy ②.applicationcontext: the whole contextual environment; Getapplicationcontext () is obtained by this method, The Getapplicationcontext () life cycle is the entire application, and the application destroys it before it destroys ③getcontext (). GetContext gets the context in which the current object resides for most scenarios, using activity or the ApplicationContext effect is the same. Activity.this to return an activity, getapplicationcontext () does not necessarily return an activity, but in some special cases, where the context of activity is used,       ApplicationContext can not be used; There are also places where you can use ApplicationContext and do not use activity: it is generally necessary to initialize the global operation.

2. After the application has been switched to the background, due to the Android memory recovery mechanism, after the activity switch to the background, because of insufficient memory, the activity was reclaimed by the system, after a period of time to return to the application, the activity was re-instantiated. 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, But Fragmenta and FRAGMENTB are still in the past, and Fragmenta and FRAGMENTB's activity has been reclaimed by the system, and this time the call to Getactivity returned NULL. Let's look at the Onsaveinstancestate method in Fragmentactivity source code:

[Java]View plain copy protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate);        Parcelable p = mfragments.saveallstate ();       if (P! = null) {outstate.putparcelable ("android:support:fragments", p); }} by the above source can be seen, fragmentactivity indeed in the Onsaveinstancestate method inside the fragment state saved. 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.
[Java]View plain copy @Override protected void Onsaveinstancestate (Bundle outstate) {//Super.onsaveinstancestate (   Outstate); }

Reference: Http://www.cnblogs.com/liuling/p/2015-9-21-1.html 3. There are some global static variables that are released to be reclaimed and opened again, because of missing values, which are easy to appear on some low-profile endpoints.

4. Also for example, jump from one interface to the second interface, there is a pass value, when the second interface when the application is switched to the background, when you switch to the foreground, in this second activity, in its onresume () method to do the data access to the previous interface to the value, When this value has been recycled, then it will be crash off, do more non-empty judgment is always advantageous and no harm.

The cause of this problem may be many, the above is my personal project experience, represents a personal point of view, welcome to Add.


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.