4

This is my sistuation

A1 = Splash Screen Activity

A2 = Main Activity

A3 = Extra Activity

S1 = GPS Service

I start with A1 that creates intent to launch A2 and then A1 finish. Inside A2 I create and bind S1 (Inside S1 I make a Notification)

CharSequence text = getText(R.string.local_service_started);

Notification notification = new Notification(R.drawable.notify_icon, text, System.currentTimeMillis());

Intent i = new Intent();
i.setClassName("xxx.yyy.zzz.kkk", "xxx.yyy.zzz.kkk.A2");
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
mNM.notify(NOTIFICATION, notification);

Now I have my icon in the notification bar If I press HOME button inside my A2 activity and open another application and then I press my notification icon all is working correctly and I gate back my A2 activity(A2 is the top most activity), but if inside A2 I launch A3 and go back HOME and press the notification I have the problems, A2 is created as a new instance(A2 now isn't top most)! Is possible to have the effect like long press HOME and focus the last open activity inside my application? I don't want to open a specific activity, but bring to front my paused activity without a new instance of the activity.

GMG
  • 1,498
  • 14
  • 20

3 Answers3

7

I have solved, this open my application preserving the actual state and get the opened activity back form background to foregraund. Do the same think of app selecting from RECENTS !

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
i.setComponent(new ComponentName(getApplicationContext().getPackageName(), MyAppClass.class.getName()));

MyAppClass is the 1st Activity your APK is launching.

Best regards

GMG
  • 1,498
  • 14
  • 20
  • It worked great for me. Will only work if one exception from the docs is met: https://developer.android.com/guide/components/activities/background-starts – Eury Pérez Beltré Mar 16 '23 at 13:58
0

You can create an Activity that finishes itself in onCreate() and start that activity from your notification.

public void onCreate(Bundle b){
  super.onCreate(b);
  finish();
  if(isTaskRoot()){
  // main Activity is not running, start it here
  }
}
Grishka
  • 2,465
  • 1
  • 20
  • 38
0

This may be a duplicate of another question, but I have not found a complete answer, so through trial and error ... You have to set the action and category. Try it out.

final Intent notificationIntent = new Intent(context.getApplicationContext(),                           MainActivity.class);
notificationIntent.putExtra("extra", "value");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction("android.intent.action.MAIN");
notificationIntent.addCategory("android.intent.category.LAUNCHER");

final PendingIntent contentIntent = PendingIntent
                        .getActivity(this, 0, notificationIntent,0); // didn't work: PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
ImplexOne
  • 549
  • 3
  • 7
  • Perfect is working for a specific intent, tank you very much, but if I also want to preserve the activity stack and only focus the top activity? How can I do thath? – GMG Jan 04 '12 at 20:35
  • Oh, works fine for me because I host all activities as fragments in main activity. Try killing CLEAR_TOP and SINGLE_TOP flags – ImplexOne Jan 04 '12 at 21:59