1

I am testing out android notifications for the first time. I am on a page with a list of contacts and I click one to trigger my notification. The notification lands me on a page with these buttons

  • snooze
  • quick text

I click snooze, then I click 10 minutes button and at this point, I call

    finish();
    System.exit(0);

which I found in this post

https://stackoverflow.com/questions/6014028/closing-application-with-exit-button

However, the app does not exit but rather goes back to the snooze and quick text options again which is very very confusing.

How do I just exit the app OR IF the user is in process of using the app, go back to the page that was open before the nofication came in? (ie. it would be preferable for the user to land back on the page with the list of contacts I think)

I am not sure it is relevant as I don't know what info is important but this is my firing notification code when user clicks a contact

private void displayNotifiation(Contact contact, byte[] img) {
    int notificationId = contact.hashCode();

    Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);

    // Create an explicit intent for an Activity in your app
    Intent intent = new Intent(mContext, ActivityNotificationLanding.class);
    Uri uri = Uri.parse("http://notexist.mykeepintouch.app/contactid/"+contact.getId());
    intent.setData(uri);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    gson.putExtra(intent, IntentFlashStore.CONTACT_KEY, contact);
    intent.putExtra(NOTIFICATION_ID, notificationId);
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Log.d(TAG, "onClick: put info="+contact.getName()+" notifId:"+notificationId);


    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, MainActivity.CHANNEL_ID)
            .setSmallIcon(R.drawable.plus_icon5)
            .setContentTitle("It's time to reach out to "+contact.getName())
            .setContentText("For your health")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setLargeIcon(bitmap)
            .setContentIntent(pendingIntent)
            .setAutoCancel(false);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);

    // notificationId is a unique int for each notification that you must define
    notificationManager.notify(notificationId, builder.build());
}
Dean Hiller
  • 17,183
  • 19
  • 103
  • 176
  • oops, just read system.exit() is not good, so scratch that part. I think I just need to capture existing activity in process IF EXIST and use that..and if not finish() and maybe it goes back to home if user didn't have app open? – Dean Hiller Jul 30 '20 at 02:53

1 Answers1

1

You can change

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

to

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

when you click button only call finish()

Uuu Uuu
  • 1,200
  • 2
  • 8
  • 17
  • thanks for the try(it's appreciated), but that didn't change the behavior. It still goes right back to page with 'snooze and quick text'. Hmmm, perhaps I need to call finish before activity the next view as well. ..... going to try that...your idea may have sparked whats going on. I think I get what finish does then maybe. – Dean Hiller Jul 30 '20 at 12:52
  • worked!!! ok, so as I go through a notification wizard, I have to call finish on each page. – Dean Hiller Jul 30 '20 at 12:53
  • Crap, new problem. In my notification wizard, I should be able to hit the back button to first page. I want to close BOTH pages when last page button is clicked but until then allow user to go back. perhaps a different SO post. – Dean Hiller Jul 30 '20 at 13:03