0

I have an Activity MyActivity. When I start this Activity, I am showing a Notification at status bar with the following details.

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_app_notify)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(notificationText)
                .setColor(Color.parseColor("#981A75"))
                .setAutoCancel(true);

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setClass(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(HomeActivity.class);
stackBuilder.addNextIntent(intent);
notificationBuilder.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

Say if I kill the app, then notification doesn't disappears. Now if I click the notification, it will come to MyActivity which has null objects everywhere. I have tried killing the activity but it doesn't bother. Any idea how I can kill this activity or if I kill the app, the notification should disappear as well.

BST Kaal
  • 2,993
  • 6
  • 34
  • 52
  • Refer [here](http://stackoverflow.com/questions/21654845/notification-shows-after-android-app-is-force-closed) – SaravInfern Nov 24 '16 at 13:21
  • http://stackoverflow.com/questions/19243137/how-to-close-any-activity-of-my-application-by-clicking-on-a-notification This might be helpful – Husnain Aslam Nov 24 '16 at 13:21
  • If your Activity has null objects when started, your architecture is wrong. Activity should resolve it's objects, or if objects are set by intent, supply them in notification intent as well. Swipe from taskbar kills Activity, not removes Notification. Anyway, if the Notification is shown from the same Activity (MyActivity), you can remove Notification by notificationManager.cancel(NOTIFICATION_ID) on Activty stop. – Yaroslav Mytkalyk Nov 24 '16 at 13:21
  • use this method finishAndRemoveTask () – kgsharathkumar Nov 24 '16 at 13:22
  • @YaroslavMytkalyk, definitely I am removing the notification if the activity ends in a proper way, but if I kill the app by swiping it from recents, the notification stays around. – BST Kaal Nov 24 '16 at 13:27
  • @kgsharathkumar, `finishAndRemoveTask ()` requires API Level 21. So I cannot ignore the possibility of apps being used on previous levels. – BST Kaal Nov 24 '16 at 13:29
  • @YaroslavMytkalyk, for the objects, I am actually checking those objects that if they are null then kill this activity, which is not working as I said in my post. – BST Kaal Nov 24 '16 at 13:30
  • @BSTKaal you might want to remove the notification from onPause(), which will be called before showing recents. – Yaroslav Mytkalyk Nov 24 '16 at 13:34
  • 2
    It would seem to make more sense for you to fix the bugs in your activity. The user will encounter this same "has null objects everywhere" in other scenarios. For example: the user is in one of your activities, then presses HOME. 10 minutes pass. Android terminates your process to free up system RAM. 5 more minutes pass. The user then attempts to return to your app. Android will fork a fresh process for you, then try to return the user to the activity they had been on before. This will result in the same crashes, and it has nothing to do with any `Notification`. – CommonsWare Nov 24 '16 at 13:37
  • @YaroslavMytkalyk, its definitely a workaround but I am not falling for that, because it I cannot remove the notification even if its in recents or by pressing home or going back , because the user will get back to the actual activity by tapping on notification. – BST Kaal Nov 24 '16 at 13:41
  • @CommonsWare, Thanks a lot for this explanation, I am facing some issues with these objects. and after reading your comment, it made me wonder more harder :( – BST Kaal Nov 24 '16 at 13:43
  • What do you mean by "killing this Activity doesn't work". Please show the code you are using to detect that you have null objects and finish. – David Wasser Nov 24 '16 at 19:15
  • So post the code that you use in MyActivity to detect it has null objects and redirect to HomeActivity – David Wasser Nov 24 '16 at 21:20

0 Answers0