I have a FragmentActivity
(called "QuizActivity") in which the user navigates through a series of question fragments, taking a quiz. Once they get to the end of the quiz, they transition to a fragment (still in the QuizActivity) that shows the quiz score. From there, I would like to give the user a button that takes them back to the activity they were on before they started the QuizActivity. I specifically do not want this button, or the back button, to just pop the back stack, which would take the user back into the questions of the quiz they have just finished.
If QuizActivity
were just an Activity
instead of a FragmentActivity
, I could call Activity.onBackPressed()
to pop the QuizActivity
off the back stack. But QuizActivity
is a FragmentActivity
, and FragmentActivity.onBackPressed()
pops fragments off the back stack, one at a time.
FragmentManager.popBackStack(id or name, POP_BACK_STACK_INCLUSIVE)
should be able to do what I want, if I can find out the id or name of the first fragment in the QuizActivity. How do I find that? The first fragment is navigated to using a navigation XML file. How do I set the name of the first fragment (so I can pop back to it, inclusive), if it's in an XML navigation doc? The android:name
attribute sets the fragment class, not the name that popBackStack would look for, I believe. Do I use android:label
?
Or how do I determine the ID? Is it the integer value (R.id.myFragment
) of the android:id
attribute in the XML navigation doc? The documentation for popBackStack(id, flags) says "The identifier is the number returned by FragmentTransaction.commit()." But since I'm not using FragmentTransaction.commit()
, what is the ID?
I tried examining the back stack to find IDs and names, using FragmentManager.getBackStackEntryCount()
, getBackStackEntryAt(i)
, and BackStackEntry.getName()
. But the entry count was always zero! I don't understand how that can happen, when there is clearly a backStack: pressing the back button moves me backward through preceding fragments (questions of the quiz).