1

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.

Community
  • 1
  • 1
Zackline
  • 804
  • 1
  • 9
  • 28
  • add the code in how you cancel the notification -- have you tried the method (public void cancel (String tag, int id)) -- as shown here -- http://developer.android.com/intl/ru/reference/android/app/NotificationManager.html – Tasos Feb 17 '16 at 20:03
  • @Tasos the problem is not the cancelation, it's when to cancel. I was not able to find a callback that would fit my requirements. – Zackline Feb 17 '16 at 20:23
  • check here -- http://stackoverflow.com/questions/5593115/run-code-when-android-app-is-closed-sent-to-background -- maybe the OnPause method will do the trick – Tasos Feb 17 '16 at 21:03
  • @Tasos I updated the question with the lifecyclecallbacks I already tried. – Zackline Feb 18 '16 at 15:16
  • If `onDestroy` was not called, the application was not yet destroyed but only moved to background. If you want to hide the notification when the user leaves your application, you need to count how many activities are between `onStart` and `onStop`. If the number is 0, the application is in background. – StenSoft Feb 18 '16 at 15:18
  • @StenSoft In my app I register a ContentObserver. As long as this Observer is active, my notification should be shown. When I close the App via swiping it out of the recent apps, the ContentObserver dies, Android Monitor in AndroidStudio shows [DEAD] next to my application (above the logcat output). Therefore I guess the activity must be destroyed, or am I wrong? – Zackline Feb 18 '16 at 15:25
  • i think any activiy (App) pauses first and then it gets killed to safeguard against a crash and loss of data. but not 100% sure. there must be something to use (Android code) to catch the pause state. i.e minimized or closed prior to kill – Tasos Feb 18 '16 at 20:55
  • @Tasos The onPause would do the job you are suggesting, but I still don't know wether my App is going to be terminated or will keep running in the background. – Zackline Feb 18 '16 at 20:59
  • @Tasos Thanks, I found what you were talking about. The isFinishing method does what you suggested. However, the onPause triggers when the user exits the App, and does not do so again when the App is closed, so it still doesn't solve my problem. Same with onStop. OnDestroy still isn't triggered at all most of the time. Such a simple task - but apparently no API for it. – Zackline Feb 19 '16 at 10:22

0 Answers0