0

Suppose I have an app where the main activity flow is A -> B. However the user may launch the app directly into B, in which case it's the only activity in the stack. I'd like the user to launch into B and still be able to reach A either through back or through the "pop stack" in the upper left corner.

The best solution for the back key is about rewriting onBackPressed(), and I understand that the "pop stack" button is supposed to slightly differ in meaning. However I'm genuinely curious about what it takes to really have to get a A -> B stack without having displayed A in the first place - that is, no "quickly flash A in the screen and launch B as fast as possible" allowed.

Fabio
  • 2,654
  • 16
  • 31

1 Answers1

2

You have two options:

  1. As you said, manipulation of B's onBackPressed() where you can start activity A through intent and finish() activity B. Here there is some sample code that you can tailor to your needs.

  2. Launch activity A whenever users requests either A or B. Then in the onCreate of your A identify the intention of the user and either call activity B (and add A in stack) or continue by showing A's UI. Note that Activities become visible only after their onCreate has finished, thus there won't be any flashes that will disrupt the user. Check out this for more information on Activity lifecycle.

Community
  • 1
  • 1
Sevle
  • 3,109
  • 2
  • 19
  • 31
  • 1
    Cool! solution 2 is exactly what I needed to know, although I was looking for another solution that wouldn't fit so smoothly: http://developer.android.com/training/implementing-navigation/ancestral.html. That's why SO is invaluable so many times. – Fabio Nov 06 '15 at 19:41