I am really moving forward with my android application. I was able to implement onDestroy() and onPause() routines.
I have also managed to get Android’s notification service to place my icon with title and body in the notification bar/task menu.
The only problem with this notification service is that if my android app is already running, and has initiated the onPause() function that super.onPause(); moveTaskToBack(true);, if a users taps the notification, it will bring up a NEW instance of my app.
Once the user interacts with the new instance, the program will crash because the background version is already running, causing conflicts.
Here’s my notification code, I need help in making this code look for an already running version of my app:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificatonManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "app name";
long when = System.currentTimeMillis();
CharSequence contentTitle = "app title";
CharSequence contentText = "text";
Intent notificationIntent = new Intent(this, SuperMicProActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int signed = 1;
mNotificatonManager.notify(signed, notification);
I was looking at onResume() with may be some sort of bringTaskToFront(this) option. Does this exist?
Thanks,
You have a few issues, IMHO.
That is rather strange behavior. Why are you doing this?
Then whatever was being done in the original activity should not be in an activity at all, but in a service. Once you no longer have a foreground activity, the odds of Android terminating your process to free up memory is fairly high and may occur rather quickly, depending on what else is going on. You seem to be depending upon that older activity somehow still doing something. A “background version” of an activity should not be doing anything that would impact any other activity in your process, and an activity not in the foreground should not be responsible for doing anything that the user might miss if the activity goes “poof”.
That being said, there are other reasons to avoid having a second copy of your activity, notably navigation — as it stands, when the user presses BACK from the second copy of the activity, they will see the first copy, and get confused.
No, you “need help in making this code look for an already running version of” your activity. An app is not an activity. An activity is not an app.
Adding
FLAG_ACTIVITY_CLEAR_TOPandFLAG_ACTIVITY_SINGLE_TOPto yourIntentwill solve this:This will bring your existing activity to the foreground and, if needed, get rid of other activities in your task.