Restoring fragment backstack for fragment with two possible backstack paths

时间:2016-12-09 13:08:43

标签: android android-fragments fragment-backstack

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?

1 个答案:

答案 0 :(得分:0)

Android开发者+ Adam Powell在这个精彩视频中找到了一些答案: Fragments: Google I/O 2016

总之,Fragments和Fragment BackStack被认为是应用程序导航状态的一部分,因此,只要我在启动活动时不会使用后端堆栈,操作系统也会恢复FragmentBackStack因此,即使重新创建活动,BackStack也会知道谁发起了C(A或B)。因此,popBackStack将根据需要从C移动到A或B.