tags:

views:

53

answers:

1

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 message 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!

+1  A: 

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"
schwiz
Thank you!!! Worked Great!
Frank Bozzo
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
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
schwiz