Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep back stack with notification

I'm trying to present a notification which is handled by a special activity which is not in the normal application flow, and trying to get the back stack handling "correct," meaning:

  1. if a notification is handled while the application is running, the notification activity should appear on the current stack, back from the notification should leave us where we were in the application. Note that this may mean the application is made open.
  2. if a notification is handled while the application isn't running, the notification activity should appear on the main (initial) activity of the application.

So far, the code I'm using to present the notification is:

/**
 * Show (or update) a notification for the current message set.
 * 
 * @param showNotification true to use a high priority notification which will be immediately
 *                         displayed (as opposed to just added to the status bar)
 */
private void createOrUpdateNotification(boolean showNotification) {
    Message oldest = messages.get(0);
    Message newest = messages.get(messages.size() - 1);

    // Create the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            // Set notification data and appearance
            .setContentTitle(title)
            .setContentText(newest.message)
            .setSmallIcon(smallIcon)
            .setWhen(newest.when)
            .setColor(ContextCompat.getColor(context, R.color.primary_color))

            // Set notification options
            .setAutoCancel(true)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setPriority(showNotification ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_LOW)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
            .setOnlyAlertOnce(!showNotification);

    // Set up the action if the first (oldest) message has an intent builder
    if(oldest.intent != null) {
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context.getApplicationContext());
        stackBuilder.addNextIntent(oldest.intent);
        builder.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
    }

    NotificationManagerCompat.from(context).notify(notificationId, builder.build());

    Log.i("notification created");
}

For clarification, Message.intent is a single intent, configured to open the notification handling activity.

My problem is that if the application is currently running and open when the notification is opened, the application is closed and the notification presented on an empty stack and the application's back stack is cleared out.

My understanding is that the desired behavior should be automatic if the content intent is a pending intent containing a single intent, which is the case here.

What am I missing?

like image 203
David Berry Avatar asked Aug 31 '25 06:08

David Berry


1 Answers

TaskStackBuilder.create will launch a fresh task stack.

Set the content intent like this instead:

builder.setContentIntent(
  PendingIntent.getActivity(
    context, 
    0, 
    oldest.intent, 
    PendingIntent.FLAG_UPDATE_CURRENT
));
like image 135
Luis Avatar answered Sep 02 '25 21:09

Luis