Android: Multiple activity instances launched by s

2019-02-25 05:00发布

问题:

I'm struggling with my app that launches multiple instances of the same Activity using the same intent. My main activity is of class type A and it does a startActivity() of two children that are of the same class type B. So we have B1 and B2 launched. If B1 and B2 are both paused (by pushing back button and making sure finish() is not invoked on them so they are truly paused), how can A uniquely bring either B1 or B2 to the foreground again? I do want to launch a new B activity. I want to uniquely bring B1 or B2 to the foreground.

so both B1 and B2 were created like this... Intent intent = new Intent(context, B.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

Now I want A to bring B1 (or B2) to the foreground/front so I use the below code, but how do I distinguish B1 or B2 when starting the activity? This only brings the last instance of B that was on top to the foreground.

Intent intent = new Intent(context, B.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent);

I've tried keeping around references to B1 and B2 and doing something like this, but this also only goes to the last instance of activity class B that was on top...

Intent intent = new Intent(B1context, B.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); B1context.startActivity(intent);

I even tried this, but it still doesnt get me my unique B1 or B2... Intent intent = B1.getIntent(); // i.e. the original intent that started me startActivity(intent); // still only brings to front the last B that was on top.

Thanks in advance!

回答1:

Thanks. Those are both good suggestions. I had considered making a C that extends B. In my app, I wanted to do things very generically since I have n number of B instances. Its not a set number. I was considering making maybe 10-20 of those simple subclasses in the AndroidManifest.xml and then managing them as they get destroyed so I could reuse them.

I didnt know about but looks similar to the subclass idea. Good to have another option to think about.

It seems very strange to me that its not obvious in Android how to deal with the history stack easier than resorting to tricks. Everything is all there except to uniquely id an Activity in the stack.



回答2:

Basically you can't. The purpose of NEW_TASK is to launch separate "applications" as the user sees them. If you are using this to try to manipulate the flow through your app, you are abusing it (and breaking things like recent tasks etc).

You can work around this by making them actual separate classes through subclasses, but before doing that please stop and think about whether what you are doing here is actually correct at all. There are only a few places where NEW_TASK should be used -- launchers, switches from notifications, etc. In all of those cases there is a pretty unique identity of what the user is launching to. Whatever you are doing should maintain those semantics.



回答3:

here is the solution :

Intent mIntent1 = new Intent(this, Activity1.class);
mIntent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
Intent mIntent2 = new Intent(this, Activity2.class);
Intent[] list = new Intent[2];
list[0] = mIntent1;
list[1] = mIntent2;
startActivities(list);