Return to currently running activity after clicking on notification

1.1k Views Asked by At

I have the following issue: I wrote an android service - a music player - that runs in the background and launches a notification at startup. Clicking on this notification opens activity A that allows to interact with this player. This works fine.

The issue is when I have third-party activity, e.g., a web browser, running and I click on the notification. This click takes me to activity A, but clicking on the BACK button - that is the issue - takes me to the home screen. Instead, I want to resume the previously running activity, i.e., the web browser.

Does anybody know how to do that?

    private void initNotification(){

    Intent resultIntent = new Intent(this, AudioPlayerActivity.class);
    //resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(AudioPlayerActivity.class);
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    stackBuilder.getIntents();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(...);
    mBuilder.setContentTitle("...");
    mBuilder.setContentText("...");
    mBuilder.setContentIntent(resultPendingIntent);


    // create a notification manager that then displays the notification
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
2

There are 2 best solutions below

2
On

Your problem is the use of TaskStackBuilder. Unfortunately, this class makes a lot of assumptions about how you want your application managed. When you use TaskStackBuilder to construct an PendingIntent stack, it adds the following flags to the "root" Intent:

 intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_CLEAR_TASK |
            Intent.FLAG_ACTIVITY_TASK_ON_HOME);

(NOTE: This code snippet is from the source for for TaskStackBuilder in Android 4.1.1)

Since Intent.FLAG_ACTIVITY_TASK_ON_HOME is set, it launches your task on top of the HOME screen instead of launching it on top of the user's current task.

If you can avoid using TaskStackBuilder, you can get around this problem. If you really need to build an activity stack, you can use PendingIntent.getActivities(), where you can control which flags are set in each Intent. Otherwise, just use PendingIntent.getActivity() to create the PendingIntent that you put in the Notification.


NOTE: Edit to add example code

Try this:

private void initNotification(){
    Intent resultIntent = new Intent(this, AudioPlayerActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(...);
    mBuilder.setContentTitle("...");
    mBuilder.setContentText("...");
    mBuilder.setContentIntent(resultPendingIntent);
    // create a notification manager that then displays the notification
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
3
On

I don't think its something that you need to handle. It depends on the application that was already running.

I just tried this out for two notifications. In the first case, whatsapp was running and then on opening a notification and pressing the back button I was taken back to whatsapp. In case two, I had chrome running and on clicking the notification and going back, I was taken back to the home screen.

In the second case, chrome probably calls finish() on its current activity which gets destroyed and there's no more activity in the backstack and hence it takes you back to the homescreen.