0

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.

GVillani82
  • 17,196
  • 30
  • 105
  • 172

6 Answers6

2

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.

kupsef
  • 3,357
  • 1
  • 21
  • 31
0

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

0

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.

sais
  • 803
  • 6
  • 15
0

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).

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • This is probably the best solution although it will only work if `Activity C` is the only way to start `Activity D`. If you have some other `Activity X` that starts `Activity D` you will have to implement the same logic there as well. – Or Bar Jan 28 '14 at 15:47
  • true. I was answering in the context of the question (the stack order was provided) – Jim Jan 28 '14 at 15:54
0

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 .

Ranjit
  • 5,130
  • 3
  • 30
  • 66
0

How about this

Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
finish();
A.Wie
  • 489
  • 3
  • 6