Can I detect if Android has killed the application

2019-01-22 16:49发布

问题:

The Android OS kills processes when it's low on memory. Scenario: Android kills the app process and I re-open it through either the Android launcher or the recent-task list (long press home button). I can check if Android killed my app process in the onCreate() method of the most recently viewed activity using:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        // Re-initialise things that killing the app would have destroyed
    }
}

However, if Android kills the app process and I re-open it through a Notification using an Intent packaged inside a PendingIntent, I don't know how to determine if the app process was killed by Android. Subsequently I do not re-initialise things that killing the app process would have destroyed.

Is there a way to determine if Android killed the application process when opening a new Activity from a Notification?

I have found one hacky solution to this problem. Using: Android: always launch top activity when clicked on notification I can open the activity on top of the stack which is passed a savedInstanceState if Android killed the app process and deal with re-initialisation. Each activity is then responsible for redirecting the user to the appropriate activity using Extras in the original Notification Intent. Intent setup for this scenario is below:

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

Is there an Action, Category or Flag I can set on an Intent that will emulate re-opening the app process as if done by the user but on a new Intent / Activity?

EDIT: To clarify the last question (although it seems my infant understanding of Android is failing me so it probably doesn't make sense): Is there an Action, Category or Flag I can set on an Intent, like in the snippet above, that will allow me to determine if the app process has been killed by the OS?

回答1:

The easiest way to determine if Android has killed the process and then created a new process is as follows:

In your root Activity (the one with ACTION=MAIN and CATEGORY=DEFAULT) create a public static boolean variable like this:

public static boolean initialized;

in onCreate() of your root Activity, set this variable to true.

In onCreate() of all your other activities, you can check if Android has killed/recreated the task by checking the state of the boolean, and if the app hasn't been initialized, you can redirect to the root Activity or call an initialization method or whatever... like this:

if (!RootActivity.initialized) {
    // Android has killed and recreated the process and launched this
    //  Activity. We need to reinitialize everything now
    ... redirect to root activity or call reinitialize method
}


回答2:

Since the process id will change when app is being killed and restarted, you can use this to check it:

In onSaveInstanceState(Bundle outState) get current process id and save it in outState:

onSavedInstanceState(Bundle outState){
   super.onSaveInstanceState(outState);
   outState.putInt("my_pid", Process.myPid());
}

Then in onCreate(Bunde savedInstanceState) compare the saved process id and the current process id:

onCreate(Bundle savedInstanceState){
   if(savedInstanceState!=null){            
      if(savedInstanceState.getInt("my_pid",-1)==android.os.Process.myPid())
         // app was not killed    
      else
        // app was killed
    }
}