After notification, I would like to exit and go back to main screen?

情到浓时终转凉″ 提交于 2020-08-10 03:39:32

问题


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());
}

回答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()



来源:https://stackoverflow.com/questions/63165441/after-notification-i-would-like-to-exit-and-go-back-to-main-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!