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