How to make notification intent resume rather than making a new intent? How to make notification intent resume rather than making a new intent? android android

How to make notification intent resume rather than making a new intent?


The idea is that people can navigate away from this activity and quickly access it again from any screen they want by pulling down the drop down menu and selecting it.

Please make this optional.

However, when the notification is pressed it starts a new instance of the activity.

That will happen by default.

What would i have to change to make it see if the activity has not already been destroyed and i can just call that instance back(resume it) and therefore not needing to load it again and won't need to add another activity to my stack.

Get rid of FLAG_ACTIVITY_NEW_TASK. Add notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); -- see this sample project.

Context context = getApplicationContext();

Please don't do this. Just use your Activity as a Context.


This is my method to show notifications. I hope it helps you.

private static void generateNotification(Context context, String message){    Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class);    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)        .setSmallIcon(R.drawable.ic_launcher)        .setContentTitle(context.getString(R.string.app_name))        .setContentIntent(intent)        .setPriority(PRIORITY_HIGH) //private static final PRIORITY_HIGH = 5;        .setContentText(message)        .setAutoCancel(true)        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);    mNotificationManager.notify(0, mBuilder.build());}


Just in case someone else has the same difficulty I had....

I tried all the code above without success - the notification item kept launching a completely new instance of my app even though one was already running. (I only want one instance to be available at any one time.) Finally noticed this other thread (https://stackoverflow.com/a/20724199/4307281), which had one simple additional entry needed in the manifest:

android:launchMode="singleInstance"

I placed that in the manifest under the main activity section of my application and then the notification item reused the already-in-progress instance.