4

1/I don't want to notify user if this one is already running my app in foreground,
is it possible before create nofification to check if my app is not in front ?

2/if app is in background, is it possible to bring last state in front ?
I know that Os can destroy some Activity, but is it possible to restore last state, don't start a new Intent?
because if i start a new Intent and i push back, old Intent appear it's not very beautiful to have 2 identical intent launch.

Thanks

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Intent notificationIntent = new Intent(this, z_finish.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification); 
NicoMinsk
  • 1,716
  • 6
  • 28
  • 53
  • possible duplicate of [How to detect if any of my activity is front-most and visible to user?](http://stackoverflow.com/questions/3136187/how-to-detect-if-any-of-my-activity-is-front-most-and-visible-to-user) – Pentium10 Aug 05 '10 at 19:32
  • Right, but you did not accept any anwser from this post, Note that my Notification are done in an Service in background and yours? – NicoMinsk Aug 05 '10 at 20:03

2 Answers2

2

is it possible before create nofification to check if my app is not in front ?

Your activities will have to tell the service when they are being paused and resumed; the service can then decide whether to display the Notification or not.

if app is in background, is it possible to bring last state in front ?

I am not quite certain what "last state" is. Try FLAG_ACTIVITY_CLEAR_TOP in your Intent.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    So in each Activity i store a variable in getSharedPreferences (for exemple) when onPause is call and modify my variable again when onResume is call, right ? i wanted to bring app in front but don't start a new Intent, i found how to do, i start a fake Activity & i make finish, so last Activity is bring in front. – NicoMinsk Aug 06 '10 at 13:42
  • I also tried this... make a fake Activity that just calls "finish", sending the user back to the last Activity. Problem is, if they press "back" until they're out of the app, they can't get back into it by using the notification (it just closes itself back to their home screen.) http://stackoverflow.com/questions/3568250/use-a-persistent-notification-to-allow-the-user-to-return-to-running-android-app – Slobaum Aug 26 '10 at 13:23
0

if your app is in singletask mode then the last state will be saved even if you relaunch it

Siddhartho
  • 39
  • 2