17

I am trying to program my notification to RESUME my app, instead of simply starting a new instance of my app... I am basically looking for it to do the same thing as when the Home button is long-pressed and the app is resumed from there.

Here is what I am currently doing:

void notifyme(String string){

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager)
                                                getSystemService(ns);

    int icon = R.drawable.notification_icon;        // icon from resources
    CharSequence tickerText = string + " Program Running...";     // ticker-text
    long when = System.currentTimeMillis();         // notification time
    Context context = getApplicationContext();      // application Context
    CharSequence contentTitle = *********;  // expanded message title
    CharSequence contentText = string + " Program Running...";//expanded msg text

    Intent notificationIntent = new Intent(this, Main.class);
    PendingIntent contentIntent = PendingIntent.getActivity(
                                                this, 0, notificationIntent, 0);

    // the next two lines initialize the Notification, using the configurations
    // above
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText,
                                                                contentIntent);
    final int HELLO_ID = 1;
    mNotificationManager.notify(HELLO_ID, notification);
}

I am guessing that the new Intent line is where the problem lies... any help would be appreciated!

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Frank Bozzo
  • 243
  • 1
  • 2
  • 6
  • possible duplicates: http://stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification, http://stackoverflow.com/questions/3356095/how-to-bring-android-existing-activity-to-front-via-notification – Philipp Feb 17 '13 at 04:13
  • this helped me http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather-than-making-a-new-intent/39482464#39482464 – TharakaNirmana Sep 14 '16 at 07:51
  • Does this answer your question? [How to bring Android existing activity to front via notification](https://stackoverflow.com/questions/3356095/how-to-bring-android-existing-activity-to-front-via-notification) – Delwyn Pinto Feb 12 '20 at 08:03

1 Answers1

18

you need to set your flags

 notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Also, if you never ever want there to be a duplicate activity give it this attribute in the manifest

android:launchMode="singleTask"
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • This only works when the app is exited via the Home Button... but does not work when the app is exited via the back button... any idea how to fix this??? – Frank Bozzo Oct 29 '10 at 00:39
  • in your notificationIntent set the action to indicate you are resuming, in oncreate check for that action and behave accordingly, pass any data you need to properly restore in a bundle in the intent – Nathan Schwermann Oct 29 '10 at 07:09
  • 1
    Reg. @FrankBozzo 's comment: default behaviour is to close/destroy the activity on back button (this is at least what `adb logcat` tells me) - so you can't resume to a killed activity... However i believe you can override the default back button behaviour (see `WebChromeClient` events) not to exit the app. – Philzen Mar 15 '13 at 01:23
  • android:launchMode="singleTask" can't believe answer from 2010 is still helpful in 2022 – Asad Mehmood Dec 09 '22 at 11:09