Chris.Zou
Chris.Zou

Reputation: 4576

Return to previous app instead of home screen from Notification

I have an app that has an activity, say A. and I put a notification to the notification bar that starts A. That is, when user select the notification, A activity will be started. But when user presses back button, the system will go to Home screen, instead of the previous activity(maybe of another app). How can i just let the system finish A activity and go back to the previous activity instead of home screen?

My code for creating the notification:

    Notification.Builder mBuilder = new Notification.Builder(this).setSmallIcon(R.drawable.icon).setContentText("title");
    Intent resultIntent = new Intent(this, ResultActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pi);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());

Upvotes: 1

Views: 796

Answers (1)

Chris.Zou
Chris.Zou

Reputation: 4576

I seem to find the answer, add the following two lines:

resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

and remove this line:

resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

But actually I don't understand why. Would anyone explain it or give a better answer?

Upvotes: 1

Related Questions