I want the notification to close once the App is closed.
I'm starting to believe it is not possible without a hack like a service checking if the App is running. The notification just opens the app when clicked, the MainActivity
is defined as SingleTask
in the manifest.
I use the following code to create the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
builder.setContentTitle("...")
.setContentText("...")
.setSmallIcon(R.drawable...)
.setContentIntent(intent)
.setOngoing(true);
notifyManager.notify(R.id.lock_notification_id, builder.build());
I already tried to cancel the notification using an LifeCycleListener
/onDestroy
in the Activity
, but the behaviour is really inconsistent, at times the notification would be destroyed, at other times it would just stay. Additionally, pressing the back key destroyed the notification but not the activity.
I'm really getting frustrated here - that task should not be that complicated. Maybe I am missing a simple flag?
Edit: I have no problem cancelling the notification. However, I don't know when to cancel it, can not find a callback that would be triggered when my application is closed. I already tried ActivityLifecyclecallbacks
in the OnCreate
of my Application
class:
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
Log.w(TAG, "Activity " + activity.getLocalClassName() + " paused!");
}
@Override
public void onActivityStopped(Activity activity) {
Log.w(TAG, "Activity " + activity.getLocalClassName() + " stopped!");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.w(TAG, "Activity " + activity.getLocalClassName() + " destroyed!");
}
});
}
However, the Destroy-Lifecyclecallback will not be triggered when the MainActivity
is destroyed.
The Logcatoutput will look like this:
SettingsActivity stopped
SettingsActivity destroyed
MainActivity paused
MainActivity stopped
But no destroyed for the main activity, which would be what I'm looking for.
Edit: Just noticed that the destroyed event is sometimes fired, in this case the notification will automatically close.
I was told in the comments that I don't receive this Callback when my app just goes to background. However, my app is clearly dead and non-functional once swiped out of the recent apps. What is the onDestroy
callback good for if not for my use case?
It was suggested to try using the Activitys isFinishing
method, however, it is of no use since the Lifecyclecallbacks
are all triggered when leaving the application, none is triggered when stopping the App, so there isn't any place where I could use isFinishing
. I also tried Runtime.getRuntime().addShutdownHook()
, but it is not triggered either.
I would also appreciate if someone can point me in the right direction on how to accomplish the task using a service. I already checked out this question, but the approaches seem to be "hacky", using deprecated methods and the documentation seems to advice against using these approaches for my use case.