Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I detect if Android has killed the application (task process) from a Notification Intent / PendingIntent?

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?

like image 946
willjgriff Avatar asked Apr 17 '15 14:04

willjgriff


People also ask

How do you know when my app has been killed?

there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.

What happens when Android decides to shut down a process?

Android might decide to shut down a process at some point, when resources are required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed.

Which method is called when app is killed Android?

When Android decides to kill our app, our activities will call onDestroy method.

How do I keep Android service running when an app is killed?

If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows: Intent intent = new Intent("com.


1 Answers

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
}
like image 141
David Wasser Avatar answered Sep 28 '22 06:09

David Wasser