0
votes

I have a Xamarin Android application with multiple activities:

I have 3 activities A1, A2, A3 that will be called from an activity X0 like this:
X0 -> A1 -> A2 -> A3

X0 can be any other activity of my app, so I don't have access to it's type. On activity A3 I have a button that need to finish A1, A2 and A3 and go back to X0.

How can I do that ?

Thanks

3
Maybe you can consider using fragment instead of Activity (since the UX are similar) - Vincent Elbert Budiman
Like having a fragment inside X0 that contain A1 then A2 then A3 ? - Hugo

3 Answers

0
votes

Since the Activity X0 is already in th back stack, you can do the following -

Intent intent = new Intent(A3.this, X0.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

With the addition of flags, all the activities on the top of Activity X0 will be closed and X0 will become visible and it won't restart too.

0
votes

Setting these flags should do the trick when you are working on the last activity (A3) in this case. Intent.FLAG_ACTIVITY_NEW_TASK Intent.FLAG_ACTIVITY_CLEAR_TASK

Alternatively, you can use fragments which will allow you to transition directly from A3 -> X0 without having to finish A1 or A2.

0
votes

Ok so I found a working solution, but it's very unclean I guess:

When I start my activity A1 inside the activity X0, I give the type of the activity X0 as a string (using flag).

Intent newActivity = new Intent(this, typeof(A1));
newActivity.PutExtra("endActivity", typeof(X0).Name);
StartActivity(newActivity);

On activity A1, I get the flag like this:

string endActivityType = Intent.GetStringExtra("endActivity");

Then when I start the activity A2 and A3 I keep giving the type of X0 as a string using flag:

Intent newActivity = new Intent(this, typeof(A2));
newActivity.PutExtra("endActivity", endActivityType);
StartActivity(newActivity);

And once I want to go back to X0 inside my activity A3, I do:

 Type typeX0 = Type.GetType("MyAppNamespace." + endActivityType);
 Intent intent = new Intent(this, typeX0);
 intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
 StartActivity(intent);

as Kashish said.