Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: restore last activity from notification

Tags:

android

My goal is to return back to last activity if user click in notification. I create the notification from in the service onCreate() in this way:

Intent notificationIntent = new Intent(this, MainActivity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

but when I click on the notification this go to the MainActivity and not to the last one opened before to click home button.

In manifest I have tried with the MainActivity with launchMode="singleTop", "standard" and "singletask" but without success.

Thank's.

like image 674
Giuseppe Avatar asked Dec 20 '22 21:12

Giuseppe


1 Answers

Just use the same intent filters as android uses when launches the app:

    final Intent notificationIntent = new Intent(context, YourActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.

like image 86
Prabu Avatar answered Jan 15 '23 15:01

Prabu