1

I have a notification in my app and I want it to bring back an existing activity when the user clicks it. The notification is genereated within the activity I want to bring back so I assume it still exists.

This is my code for the notification:

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification noti = new Notification(R.drawable.icon, "s", 0);
    CharSequence title = "S";
    CharSequence details = "W";
    Intent intent = new Intent(getBaseContext(), Start.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
    PendingIntent pending = PendingIntent.getActivity(getBaseContext(), 0, intent, 0);
    noti.setLatestEventInfo(Start.this, title, details, pending);
    nm.notify(0,noti);

But for some reason it keeps creating a new activity. what am I doing wrong?

AshChlor
  • 143
  • 1
  • 13

1 Answers1

1

"The notification is genereated within the activity I want to bring back so I assume it still exists." Well, it may, but it's not generally a good idea for you to count on that. What if the user clicks on the notification only after backing out of your app using the back button, or after some length of time during which the OS has killed your activity while the user has been doing other things? Is there a particular reason why you need the "old" activity? If it has a state that you need to preserve or recreate, you can do that: have a look at http://developer.android.com/training/basics/activity-lifecycle/recreating.html and http://developer.android.com/guide/components/activities.html#SavingActivityState.

hBrent
  • 1,696
  • 1
  • 17
  • 38
  • is there a way to check if the activity still exist and then act accordingly? – AshChlor Nov 04 '12 at 05:28
  • @AshChlor, You should be able to tell if your activity hasn't been killed. When an activity is killed, its onDestroy method is called. Therefore, you could put some code into onDestroy that you can use to check that that method has been called. Of course, because the activity is destroyed, you can't just use a variable defined in that class--you have to use a variable outside your activity (e.g., in a class that extends the Application class) or a Shared Preference. A boolean works well for this--you'd set it to true in onCreate in your activity, false in onDestroy. – hBrent Nov 06 '12 at 03:06
  • @AshChlor, Continuing my answer above, you should note that you should be able to recreate your activity in the state it was in before (as I noted in my answer), so that trying to bring up the existing activity shouldn't be too important. See http://stackoverflow.com/questions/3667022/android-is-application-running-in-background for an example of how to use the Application class to keep track of the status of an activity, though note that that answer applies to paused and resumed activities, not created and destroyed. – hBrent Nov 06 '12 at 03:11
  • @AshChlor Also look at http://developer.android.com/guide/components/tasks-and-back-stack.html and http://developer.android.com/guide/topics/manifest/activity-element.html, in particular the section "android:launchMode". If you put `android:launchMode="singleTop"` in your manifest, you'll tell Android that you want it to reuse your activity when possible, rather than creating a new one. – hBrent Nov 06 '12 at 18:53