0

I have the following stack of activities on an Android application:

A -> B -> C

On C, I can go back home ("A") using startActivity with the Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP intent flags. This clears the stack out of activities B and C so it's just A. Fine so far; this is my equivalent of "home".

However, I have another section - "E" - and I want it to clear the whole stack except for home ("A"). As in:

Activity stack: A -> B -> C
(User clicks on "E" button)
Activity stack result: A -> E

I've tried using Intent.FLAG_ACTIVITY_NEW_TASK when opening subsection ("B", "E", etc) but there's no way to clear the previous task it seems. I've also tried Intent.FLAG_ACTIVITY_TASK_ON_HOME but it doesn't seem to do anything (how do you even set what's "home" anyway?).

Is it possible?

zeh
  • 10,130
  • 3
  • 38
  • 56
  • 1
    If you don't need to return to a previous `Activity`, why don't you just call `finish()` in each `Activity` immediately after calling `startActivity(...)` to start the next? – Squonk Nov 09 '12 at 00:09
  • 1
    Hi. A similar question with answer can be found here: http://stackoverflow.com/questions/3473168/clear-the-entire-history-stack-and-start-a-new-activity-on-android . :) If you're using API < 11, then you have no choice but to create your own logic to clear the stacks. But if you're using API >= 11, you can just use `Intent.FLAG_ACTIVITY_CLEAR_TASK`. – Arci Nov 09 '12 at 06:06
  • Squonk: because I may have a trail of activities before deciding to go back to a different path. In my example, `finish()` would only close C, not B and C. – zeh Nov 09 '12 at 15:37
  • Arci: you're right, that seems to do the trick. However, my app supports API v8, so I guess that's why I hadn't ran into it (and why I can't use it...). Appreciate the answer though. – zeh Nov 09 '12 at 15:40

1 Answers1

1

A not too complicated solution would be to add an extra to the 'clear top' Intent you're passing to Activity A. Based on that information you can then take action whenever Activity A receives a new Intent.

For example. you could set up a series of 'commands' that you identify when receiving an Intent in Activity A and based on which you then do something; i.e. start Activity E.

Alternatively, since and Intent is parcelable, you could attach the Intent that starts Activity E as an extra to the 'clear top' Intent you send to Activity A. Rather than identifying a command, you can then simply check if there's a 'forwarded intent' and call startActivity(forwardedIntent).

In all these cases the clear top flag will result in B and C being popped, leaving A. Activity A will then take care of launching E. The flow should be fast enough to not result in any visual flashes, e.g. from Activity A briefly displaying before Activity E takes over.

I'm not sure if the behaviour you're after will make sense to the user, but I'll leave that up to you.

MH.
  • 45,303
  • 10
  • 103
  • 116
  • Thanks MH. I initially thought this would be an odd solution but it actually works well. I'm just running my "main menu" intent now and passing an additional "command" as a parameter. The main menu activity receives the new intent on `onNewIntent()` and then opens the new activity. It all happens invisibly, so no glitches. Also: the behavior may seem odd, but it makes sense because almost all activities on my application have a sliding menu. Going from C to E means the user opted to go to a different section of the application, so going back should go back to the main menu activity, not C. – zeh Nov 09 '12 at 15:30