31

is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.

Thanks

gMale
  • 14,075
  • 17
  • 81
  • 109
Waypoint
  • 15,705
  • 36
  • 110
  • 167
  • 3
    Why would you need to do this? Only one can be active at a time, so even if you did many to do it, only the last one would be show and others would be paused. – Michell Bak Oct 07 '11 at 07:35
  • 1
    Absolutely agree with the upper comment. You should rethink your app's architecture, why would you want to start an activity that will not be available to the user? – Egor Oct 07 '11 at 07:42
  • 1
    why do you need to start multiple activities at once? – Terence Lui Oct 07 '11 at 07:42
  • 4
    Because I need to resume application state, when it was closed. And if the user was seeing activity C, I need to open A, B, C (which will be shown) – Waypoint Oct 07 '11 at 08:02
  • 1
    The Android system will take care of remembering the stack of activities, under normal circumstances. Are you wanting to preserve the stack across reboots or something like that? – Kevin Reid Oct 07 '11 at 20:20

4 Answers4

43

You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.

Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);

Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);

Intent k = new Intent(this, C.class);
startActivity(k);

In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.

Raginmari
  • 1,575
  • 15
  • 18
  • thanks, its working. But i have another requirement. I have 2 activities A & B. I need to use startActivityForResult to start A from B. If i do this, onActivityResult in A is not getting called. Is there a way? – kishorer747 Sep 20 '16 at 12:50
  • seems like @gmale can help with this – Ewoks Jan 17 '17 at 16:39
  • Sorry but what it shall do? I'm having Log in `onCreate()` of secondary activity (with set `FLAG_ACTIVITY_NO_ANIMATION`). And `onCreate()` of that secondary activity seems never called. Is it suppose to work like that? And can I call `startActivity(secondary)` from `onCreate()` of my main activity? How do I call `onCreate()` of secondary activity? – Vit Bernatik Oct 02 '17 at 17:05
22

This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.

Take a look at TaskStackBuilder. One common example:

TaskStackBuilder.create( context )
        .addNextIntent( intentOnBottom )
        // use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
        .addNextIntentWithParentStack( intentOnTop )
        .startActivities();
Tuan Chau
  • 939
  • 13
  • 25
gMale
  • 14,075
  • 17
  • 81
  • 109
  • Just for awareness. If we start 2 activities, let's say A1 then A2, if A1 has a complicated UI like ViewPager + Fragment, or background load, this solution may end up with a crash while starting A1 then A2 with separated `startActivity(Intent)` works well. – Tuan Chau Dec 21 '20 at 02:41
15

Really old question but I thought I still answer it.

Use: public void startActivities (Intent[] intents, Bundle options)

2

Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity. This will immediatly launch a new Activity and pause the first one. With back-key you will get back to the last Activity

Thommy
  • 4,644
  • 2
  • 23
  • 43
  • 2
    The issue with this approach is that if the activity B got killed (finished) by the system after starting activity C, and when the user comes back to the activity B by clicking Up button, the activity B's onCreate will try to start activity C again. – tmin Jan 21 '14 at 02:06
  • @tmin that probably could be solved with 'savedinstancestate' – algrid Mar 15 '18 at 17:32