5

Possible Duplicate:
Removing an activity from the history stack

Suppose I have three activities on my stack, like this A → B → C.
I would like to terminate B and replace it with an activity D, so that the stack looks like this : A → D → C.
The intended effect is to have a "screen chooser" activity C. It opens on top of activity B, offers a number of things to choose from, and when the user clicks one, it closes, revealing the chosen screen. I can cancel the opening animation with overridePendingTransition, but that alone won't help me reveal the activity by closing the chooser interface.

Is there any way to do that in Android ?

Community
  • 1
  • 1
Jean
  • 10,545
  • 2
  • 31
  • 31

3 Answers3

2

You could start B with FLAG_ACTIVITY_NO_HISTORY flag. So it will not go to the stack and C will make a decision to start D or B. Does it fit what you need?

ackio
  • 662
  • 1
  • 5
  • 12
  • No, it doesn't for two reasons. The first one, it will prevent the user to return to it from the screen chooser with the back key. The second one, it doesn't change that it will open the new one on *top* of the screen chooser, which is exactly what I am trying to avoid. – Jean Nov 12 '10 at 12:14
  • Look, you can start needed activity in the chooser's onDestroy(). If nothig is changed in chooser - it starts B, otherwise it starts D, and closes itself. So on back key onDestroy() will be called and you "return" to B activity. How it looks? – ackio Nov 13 '10 at 05:11
  • In general it is not good idea to change the stack and it's the unexpected behavior for android user. But if you still want it this hack could help. – ackio Nov 13 '10 at 05:23
1

Another idia)) B and D are the same activity(!) which has B-mode and D-mode with defferent layouts. When it's in B-mode it starts C with StartActivityForResult(). And on returning it decides which layout to inflate. Pretty easy, right?

ackio
  • 662
  • 1
  • 5
  • 12
  • This does work. But I'm working in a pretty big team, and we develop a dozen of different screens - putting them all in the same activity is very inconvenient, to say the least. That being said, I can't think of any other decent way, short of having the chooser take a screenshot and pass it to everyone else and requiring to display it first with a closing animation and... yuck. Anyway thanks a lot, I don't think it can be done any other decent way than making it a single activity with several modes. – Jean Nov 15 '10 at 09:57
1

I have two starts of real anwsers. The first is not perfect but very easy to implement; the second one should give the expected results but is harder to implement.

One is to start activities with the FLAG_ACTIVITY_REORDER_TO_FRONT and FLAG_ACTIVITY_PREVIOUS_IS_TOP. This will give the expected behavior for any existing activity. It still does not work if the activity is not already in the back of the stack, notably on its first start.

Another possibility - which I have not implemented yet - is to create an ActivityManager very much like a TabHost and pass intents to this guy. It then takes the responsibility of loading the next activity in its onNewIntent() method. This should work and allows for separate development of separate activities.

Jean
  • 10,545
  • 2
  • 31
  • 31