21

I'm developing an app which shows a notification to the user. The notification's objective is to make it easy to the user to return to the activity when the user is in another activity. I am using this code in my app to create and show the notification.

                    notification = new Notification(R.drawable.icon,
                            "Notify",
                            System.currentTimeMillis());
                    notification.setLatestEventInfo(this, "App name",
                            "App message",
                            PendingIntent.getActivity(
                                    this, 0,
                                    new Intent(this, Main.class),
                                    PendingIntent.FLAG_CANCEL_CURRENT));
                    notification.flags |= Notification.FLAG_ONGOING_EVENT;
                    nManager.notify(0, notification);

But when the user taps the notification starts a new instance of the same activity, instead of the one that the user was using before.

I think this has something to do with PendingIntent but I can't find how to make that Intent to resume a previously paused instance of the activity instead of creating a new instance.

Thanks.

Jimix
  • 1,499
  • 3
  • 15
  • 20
  • 1
    did you find a solution for this problem ? what i've found is a workaround : all activities extend a base activity to store the number of active activities (+1 for onCreate , -1 for onDestroy) , and the notification starts a fake activity that runs on a new task, which checks the number of running activities , and if it's >0 , it just closes itself , and if it's ==0 , it starts the first activity . – android developer Jan 12 '13 at 19:22

5 Answers5

22

I found out how to do it. I added the following code:

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Now my code looks like this:

    notification = new Notification(R.drawable.icon,
            "Notify", System.currentTimeMillis());
    notification.setLatestEventInfo(this, "App name",
            "App message", PendingIntent.getActivity(this,
                    0, new Intent(this, Main.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                    | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    PendingIntent.FLAG_CANCEL_CURRENT));
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
Jimix
  • 1,499
  • 3
  • 15
  • 20
  • Jimix, I tried with your code but didn't work in my case, this open the MainActivity instead the last one focused. Probably my error is in the manifest, how do you set the mainactivity ? – Giuseppe May 14 '12 at 11:45
  • Those code should work. Check other activities in the same task stack. They may involve task order. – Kislingk Feb 13 '14 at 17:04
9

This helps for me android:launchMode="singleInstance"

<activity
        android:name="com.mosis.automatskidnevnik.MainActivity"
        android:launchMode="singleInstance"
        android:label="@string/app_name" >
        ...
Pang
  • 8,605
  • 144
  • 77
  • 113
Zeljko Ivanovic
  • 373
  • 3
  • 5
  • Avoid using this and singletask if you have multiple activities, this can be used for just 1 activity , but use flags instead if you need to resume an Activity or have some conditions to launch it, the problem here is that if you need more instances in the future to send extras or data, this will prevent you to do it – Gastón Saillén Jan 23 '19 at 14:39
1

I solved the same problem by setting

android:launchMode="singleTask"
Pang
  • 8,605
  • 144
  • 77
  • 113
Abhishek Jaiswal
  • 588
  • 5
  • 17
0

Apart from the correct answer of @Jimix I would change the PendingIntent requestCode from 0 to a non zero value as commented on another answer. On the other hand, there is a known bug on previous Android versions that took me a couple of desperate hours. The solution proposed there worked fine for me cancelling the PendingIntent before of sending the notification:

if (Build.VERSION.SDK_INT == 19) {
   getNotificationPendingIntent().cancel();
}
Community
  • 1
  • 1
GoRoS
  • 4,445
  • 2
  • 37
  • 57
0

When creating your PendingIntent, you use:

Main.this.getBaseContext()

You should use the default Activity context if you want to return to the same activity. Read more here:

Android - what's the difference between the various methods to get a Context?

Community
  • 1
  • 1
Yossi
  • 1,178
  • 1
  • 15
  • 30
  • 1
    I tried using the default context with "this" in the PendingIntent but it didn't work. – Jimix Mar 09 '11 at 16:04