Alpar Torok Alpar Torok - 4 months ago 22
Android Question

How to bring application to front when the notification icon is clicked (from Service)?

I have an application that plays media with the help of a service. This runs smoothly with the help of a service ( ongoing notification is created ). When the back key is pressed the application goes to background. The problem is when I click the notification icon a new instance of my application is created every time. How can I get my application from the background to the front with the help of the notification bar?

My notification looks like this:

private void notifyMe() {
final NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon_audio, "Online!",
note.flags = Notification.FLAG_ONGOING_EVENT;
PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, PlayMedia.class), 0);
note.setLatestEventInfo(this, "Media Payer", "Online",i);
mgr.notify(NOTIFY_ME_ID, note);

I got it to work by adding the following line in the manifest file: android:launchMode="singleInstance"
The intent flag didn't have any effect, thought that was odd...


The intent that you pass to PendingIntent.getActivity needs to have the appropriate flag set to bring a running application to the front. So:

Intent startActivity = new Intent(this,PlayMedia.class ); 
startActivity.setFlags(Intent.*appropriate flag*); 
PendingIntent i = PendingIntent.getActivity(this, 0, startActivity, 0);

Go to and search (ctrl+f) for "flag_". There you will find a list of the flags you can use.