6
votes

In an android app, I create a notification when an app is moved to the background. When the notification is clicked, it moves the application back to the foreground.

The problem I have is in the background, and the user removes the app through "Recent apps" (i.e., he swipes it away), the notification stays. How can I capture the swipe event, i.e. when the app is removed from "Recent apps". I tried onDestroy, but that is not getting triggered.

1
No way. Device should run fast, so such deep callbacks are not supported in Android at all. Also, "Recent apps" has nothing to do with activity lifecycle.weaknespase
Doesn't "Recent apps" remove the app from the memory? I have trouble understanding what "Recent apps" is and how it functions.alexb
It is just "Recent apps" list. All it does - remembers any activitity which successfully executed onPause callback. Also it provides a way to switch between recently opened apps without requirement to go deep into apps list, function to remove some apps from list just for your convenience. Other from routine that catches launches and makes screenshots, it has no relation to activity or application lifecycle. You misplaced it with task managers.weaknespase
I know it's an old question, but you could keep a Service running when your app is on background and use onTaskRemoved to detect when the user closed de app via Recent Apps swipe.Marina.Eariel
thanks @Marina.Eariel, works great!Matias Elorriaga

1 Answers

0
votes

Added in API level 14, there is a callback method onTaskRemoved. This is called if the service is currently running and the user has removed a task that comes from the service's application.

So in your Service class, who is posting the notification, you can override this method and stop the running service which is responsible for showing the notification.

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        stopSelf();
    }

With above code inside your Service call, as soon as user removes the app through "Recent apps" (i.e., he/she swipes it away), the onTaskRemoved callback for this service will be invoked and with stopSelf() app is stopping the service responsible for showing/posting the notification.

Consequently the posted notification will be removed by the system.