11

When my stack is in this situation:

A->B->C

if I start D activity, I want that the activity stack becomes:

A->D

Note that activity C is a dialog.

Bart
  • 19,692
  • 7
  • 68
  • 77
Michele
  • 1,213
  • 2
  • 18
  • 36

6 Answers6

5

here are the steps which will do the needed:

  • from activity C launch the activity A with a boolean fromActivityC bundled with the intent and the flag FLAG_ACTIVITY_CLEAR_TOP set .
  • Now in the on create of the activity A check for this boolean "fromActivityC" first and if present launch the activity D else the normal flow continues.

    // following code can be used to get the boolean in the oncreate
    boolean entrypoint=this.getIntent().getExtras().getBoolean("fromActivityC");
    

A lil workaround but Hope it helps

kenju
  • 5,866
  • 1
  • 41
  • 41
Nitin
  • 1,451
  • 13
  • 17
  • Thanks, interesting trick, it runs ... But typing on keyboard I had an idea: when A calls B, finish A; when B goes back, recreate A; when C calls D, create D with FLAG_ACTIVITY_CLEAR_TOP. Yessss!! – Michele Jun 16 '11 at 13:32
  • 1
    yes that should work .. but dont you think re creating activities like this everytime will take a lot of cpu cycles and defeats the very purpose of the activity stack..something to ponder upon :) – Nitin Jun 16 '11 at 13:35
2

There is several way to remove a activity from the stack or prevent it to be stacked :

To remove your activity from the stack , simply call finish(), see here.

You can also implement in your manifest the property : android:noHistory="true" which prevent an activity to be stacked.

See this question form more detail : Removing an activity from the history stack

Community
  • 1
  • 1
grunk
  • 14,718
  • 15
  • 67
  • 108
1

A little hacky, but you could start activity A while clearing the stack, then start activity D.

Activity A might appear for a moment before disappearing though

Kurru
  • 14,180
  • 18
  • 64
  • 84
1

You could try finishing ActivityB when you launch ActivityC and the same in ActivityC when launching A.

Your code would look like this:

Intent i = new Intent(this.class.ActivityC);
startActivity(i);
ActivityB.finish();
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
martincm
  • 13
  • 1
  • 6
0

on callback of C , pass a message back to A and start your D from Acall finish , this will finish B and C (because C is apart of B).

Adeel
  • 859
  • 6
  • 9
0

here is a better solution not depending on evaluating the Intent content in A: use the flag FLAG_ACTIVITY_TASK_ON_HOME

you should also take a look at the other flags Android Intent

A. Binzxxxxxx
  • 2,812
  • 1
  • 21
  • 33