Android notification to bring back existing activity


Ask by : AshChlor November 03, 2012 20:50

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?

View original question

Answer by : hBrentNovember 03, 2012 20:50

"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.

View original answer