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!",
System.currentTimeMillis());
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);
}
THE SOLUTION 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...
1 Tap Notification Settings on the notification panel or tap the Settings app. 2 Tap Notifications. 3 Tap App icon badges.
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 http://developer.android.com/reference/android/content/Intent.html and search (ctrl+f) for "flag_". There you will find a list of the flags you can use.
Just a variation on the answer and a comment on the question. I didn't need to add android:launchMode="singleInstance" as Alpar suggests. This is what I did to make an app come to the foreground.
// this code brings the application from background to foreground
// when the Notification icon is clicked on.
// create new notification
int icon = R.drawable.ic_notify;
Notification notification = new Notification(icon, "Ticker Text", System.currentTimeMillis());
// Create new Intent pointing to activity you want it to come back to
Intent notificationIntent = new Intent(this, MainApp.class);
// Add new intent to a pending intent
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// add that pendingIntent to the new notification
notification.setLatestEventInfo(getApplicationContext(), "Title", "Text", contentIntent);
// send the intent to the NotificationManager
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(UPLOADER_ID, notification);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With