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.
You don't need to clear your apps on newer phone. Android will manage it memory. If you clear your apps to often, it will only slow down your phone and make it work harder thus run out of battery faster.
To remove a persistent notification on Android as fast as possible, first, press-and-hold on it. Alternatively, swipe the notification left or right to reveal a gear icon on either side, and then tap on it. The notification expands. Tap on “Turn off notifications” at the bottom.
To dismiss a notification, touch it and swipe left or right. Tap the dismiss icon to dismiss all notifications. On newer versions of Android, you can manage some notifications from the lock screen. Double-tap a notification to open the app or swipe left or right to dismiss the notification.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With