2

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());
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
Martin
  • 361
  • 2
  • 8

2 Answers2

5

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());
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 2
    Great info. Have never used this flag before. – Shivam Verma Jun 17 '15 at 14:42
  • Hi David! Thanks for your answer. I tried to omit the TaskStackBuilder and work with PendingIntent.getActivities(), but the result remained the same. Could you briefly outline - maybe with some pseudo-code - how to use the taskStackBuilder in combination with PendingIntent.getActivities()? Thanks a lot in advance. – Martin Jun 18 '15 at 06:38
0

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.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • Thanks for the quick response. I tried it on my device with Firefox and a notification from a background process (task manager). It then returns to Firefox. However, when I click on the notification generated by my app, it returns to the main menu. – Martin Jun 17 '15 at 12:19
  • 2
    No, this is actually is behaviour that you can control. It all depends on what flags you set in the `Intent` that you put in the `PendingIntent` that you put in the `Notification`. – David Wasser Jun 17 '15 at 14:38
  • Thanks again! I will try that tomorrow out and let you know how this worked out. – Martin Jun 18 '15 at 22:36