androidapplication-restart

Android Application Crashes when RAM Memory Cleared


I have an app that crashes after the RAM memory is cleared. I cannot use the onSavedInstanceState because of the current implementation. So, does anybody know, how could I just restart the app when the user tries to open it from Recent Apps? I already tried this code in the Main activity which is the base class for all activities:

if (isFirstApplicationStartUp()) {
        Intent i = new Intent(this, Main.class);
        startActivity(i);
    }

isFirstApplicationStartUp() is a boolean set to true from a class which extends Application (in onCreate).

But this implementation does not work as desired as the previous activities get to be called before this code is executed.


Solution

  • 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.

    (doo dah)