Android Application Crashes when RAM Memory Cleared

风流意气都作罢 提交于 2019-11-28 14:23:12
David Wasser

You probably don't want to have the app restart from the beginning when being launched from the "recent tasks" list, because your app may be perfectly capable of working. What you need to do is you need to remember if your app has been properly "initialized" (whatever that means). If the user returns to your app and the process has been killed and restarted since your app was initialized, you need to detect this condition and then redirect the user back to the first activity of your app.

The best way to do this is to have a base class for all of your activities. In this base class you implement code in onCreate() that checks if your app has been properly initialized or not. If it has not been properly initialized, you should redirect the user back to the first activity. Something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Check if the application has been restarted by AndroidOS after it has killed the process due to inactivity
    //  in this case, we need to redirect to the first activity and dump all other activities in the task stack
    //  to make sure that the application is properly initialized
    if (!isApplicationInitialized() && !(this instanceof FirstActivity)) {
        Intent firstIntent = new Intent(this, FirstActivity.class);

        firstIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // So all other activities will be dumped
        startActivity(firstIntent);

        // We are done, so finish this activity and get out now
        finish();
        return;
    }

    // Now call the activity-specific onCreate()
    doOnCreate(savedInstanceState);

All of your activities need to inherit from this base class and they should NOT override onCreate(). Instead, they should implement the method doOnCreate(), which will be called from the base class' onCreate() (see above).

NOTE: This only works if the root activity (in this example FirstActivity) is never finished until the app quits. This means that you will always have an instance of FirstActivity at the root of your task. This is required so that Intent.FLAG_ACTIVITY_CLEAR_TOP works correctly.

The android OS is responsible for clearing up ram when it needs it. This means it can liberally kill any app when it is not in use. The app itself tries to save the view components of the app but you are responsible for saving any other variables or restoring any images.

Here is a more detailed explanation: http://www.senzumbrellas.com/collection/home.php?sl=en

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.


Instead of setting this boolean, Android gives you a way to access any instance information. You should instead override onSaveInstanceState(Bundle savedInstanceState):

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    //save any state variables you want to keep around.
    //this is not rally meant for large memory intensive objects like images

    super.onSaveInstanceState(savedInstanceState);
}

Then in onCreate check savedInstanceState in onCreate:

if(savedInstanceState != null)
{
    //this means that the activity is being restored
}

use shared variables instead of global variables in the activities. This solved my problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!