GVillani82
GVillani82

Reputation: 17429

Finishing activities when new activity is started

Supposing I have an Activity Stack like this(from top to bottom):

D - C - B - A

So the activity D is on top of the stack and is currently running (displayed).

Now, from the activity D I want call the activity B (in the second position in the stack), so:

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

After this operation, the new configuration of the stack, now should be:

B - D - C - B - A

But what I really want is the following stack configuration:

B - A

How can I obtain this result? Is there any FLAG that I can use for this?

Summarizing I want that when I start the activity B (from D) both D and C are automatically finished.

Upvotes: 0

Views: 92

Answers (6)

A.Wie
A.Wie

Reputation: 499

How about this

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

Upvotes: 0

kupsef
kupsef

Reputation: 3357

That is what FLAG_ACTIVITY_CLEAR_TOP for.

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

Upvotes: 2

Ranjit
Ranjit

Reputation: 5150

Unfortunately there is no flag to fulfill your requirement.

As Tober said:

What you can do is you just finish C before D and do same when calling the next one.

In my view the first answer is same as tober like call finish in both C and D but now in A when you acivities are going in onPause stage .

Upvotes: 0

Jim
Jim

Reputation: 10278

you need to use "startActivityForResult" and then when you want to "call" activity B you actually "finish" activity D. From there, do a check in Activity C to see what you should do next:

How to manage `startActivityForResult` on Android?

You will want to pay attention to the part where you set data in your intent - that will tell you if you should finish Activity C or something else.

RESULT_OK and RESULT_CANCELED don't tell you much, and also be careful because hitting "back" can result in a null intent (so you can't rely on checking for the contents of the result intent, if that intent is null).

Upvotes: 0

sais
sais

Reputation: 843

Call "C" from "D", with a intent variable like

Intent intent = new Intent(this, ActivityC.class);
intent.putExtra("BYPASS_ME", true);
startActivity(intent);

In ActivityC.java, get the intent value, if its true then finish the activity. Now C will be removed from stack.

Upvotes: 0

user3211432
user3211432

Reputation: 11

you can use startactivity for result when starting d and send intent value as "ActivitStarted","D"

onresult

check result activity send d if d send then again start c check result activity send c if c send then again start b check result activity send b if b send then again start a

Upvotes: 0

Related Questions