When my notification goes off I want to restore the activity that was put into the background, not start a new activity. I've seen some answers about using FLAGS but I don't know how to implement it
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | INTENT.FLAG_ACTIVITY_SINGLE_TOP);
Where do I put this in my code? I tried but it didn't work. Please help!
ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.icon;
tickerText = "Short Msg";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
context = getApplicationContext();
contentTitle = "MyApp";
contentText = "Reopen App";
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationIntent = n开发者_如何学Goew Intent(this, StartTimer.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Figured it out, set the Activity to SingleTop or SingleInstance in Android Manifest, then instead of creating a new activity it just reopen the one still active.
Note that answer marked as correct is not completely correct as "singleTop" still may create multiple instances of your activity under certain conditions.
The launch modes that really are garanteed to create a UNIQUE instance of your activity at any condition are "singleTask" and "singleInstance".
These two options creates one and only task for your activity being the root of the task, with the difference that "singleInstance" don't allow other activities on top of yours, while "singleTask" does.
Source: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
精彩评论