0

I have one Activity with 3 fragments which form a workflow to collect user input. Normally, Fragment A is the first fragment -> Launches B -> Launches C. B is supposed to launch A if the back button is pressed, and similarly C's Back button is supposed to launch B.

However, in some cases, A is supposed to launch C directly, and then C's back should launch A again.

I prefer that C should not know who launched it. I.e. I want C's "backstack" to operate without C knowing who launched it.

I tried using the usual addToBackstack approach, but I'm facing a problem when the Activity gets killed after the user lets the app go into the background while C was open.

  1. I would like the user to return to "C" instead of starting all over from A. To achieve this I'm using the saved Instance state, and detecting which fragment was previously active, and launching it from the activity.
  2. The problem starts when the user wants to go back from C, after the Activity was recreated after being killed. C doesn't know who launched it: A or B. How do I make the Back button work as expected for this case?
user90766
  • 329
  • 1
  • 4
  • 17

1 Answers1

0

Found some answers in this excellent video by Android Developers + Adam Powell: Fragments: Google I/O 2016

In summary, Fragments and the Fragment BackStack are considered part of the navigational state of the app, so, as long as I don't mess with the backstack when the activity is launched, the OS will also restore the FragmentBackStack, thus the BackStack will know who launched C (A or B) even if the activity gets re-created. Thus, popBackStack will move from C to A or B as required.

user90766
  • 329
  • 1
  • 4
  • 17