3

I wish to have an app that targets Android 2.1, API level 7, launch multiple activities at once when a user clicks on a C2DM notification that has come in. This is the method I currently use to launch my activity:

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)

This method only allows me to put one activity on the stack. What I really want to do is use this method:

public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)

This method reports that it is only available for API level 11, which is Android 3.0. I do not wish to break backward compatibility with 2.1. Can anyone suggest how I might be able to achieve this effect without taking a dependency on Android 3.0? I tried looking for the source to this new method, but it does not appear to be available yet.

esilver
  • 27,713
  • 23
  • 122
  • 168
  • "...launch multiple activities at once..." - Why would you want to do this? Only one activity can be seen (and in most cases be active) at any given time. – Squonk Apr 21 '11 at 05:34
  • @esilver what did you end up with , i am facing the same problem What did you did eventually . please help – user4o01 Sep 22 '12 at 18:12
  • Basically I add a special boolean flag to the intent and always launch my root Activity. The root activity checks for the special flag, and if it is present, it wil launch the second activity and put it onto the stack. – esilver Sep 23 '12 at 02:36
  • @esilver I tried adding a boolean 'extra' to my Intent which solved the problem. But now every time I launch the main activity (i.e. the first one) from the recent activities, the intent always has that extra. I even tried Intent.removeExtra, but that didn't seem to help. Did you do something differently? – Gautam Sep 29 '12 at 07:02
  • Try datestamping or putting a guid on it and persist the ones you've seen already. – esilver Sep 30 '12 at 15:46

2 Answers2

1

What you do is have a separate activity that is the target of the alarm, and build the intent stack from there, as below. This could probably be generalised into something very like 'getactivities' quite easily - it's a pity it isn't in the compat libraries.

public class AlarmActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

    intent = new Intent(this, ChildActivity.class);
    startActivity(intent);

    finish();
  }
}
Tony Hoyle
  • 609
  • 6
  • 4
-1

As MisterSquonk says in the comments, only one Activity can be active at any one time (even in 3.0), so launching "multiple activities at once" is not going to be possible. Even if it were, what will the user experience be like with multiple activities starting in quick succession, and no guarantee of which will be launched last, and so be the one left for the user to interact with.

I suspect that you actually want to wake up different parts of your app simultaneously without each one having its own UI. If so, then I would suggest having one or more Services which implement multiple BroadcastReceivers against a common Intent filter. When you fire a Broadcast of that event, then multiple things will get woken up at once.

Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • 2
    I have a messaging UI activity and an inbox activity. I want the notification to bring you directly into the message, and the back button to bring you to the inbox, and then the next back press to take you to where you were. I don't believe having multiple services react to the same broadcast is correct, because I won't have any guarantees of the order in which the activities are shown. – esilver Apr 21 '11 at 15:47
  • 1
    In that case you need to fire an Intent to launch the inbox activity with an extra detailing the message id. Then the inbox intent should launch the message view activity. That way you'll get the back button behaviour that you want. – Mark Allison Apr 26 '11 at 08:41