1

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).

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • Why not just have the button start a new intent to go to the desired activity? You could put extras in the intent to keep track of the state they are in – Quinn Nov 13 '19 at 21:27
  • 3
    It sounds like you want to pop the whole activity, correct? That's what the `finish()` API for Activity does. Is there a reason you aren't doing that? – ianhanniballake Nov 13 '19 at 21:27
  • @Quinn I considered that, but how do I know what desired activity to go back to? They could have come to the current activity from a couple of different places. – LarsH Nov 13 '19 at 22:39
  • @ianhanniballake Yep you're right. (Though I'd still like to know why `FragmentManager.getBackStackEntryCount()` was giving zero.) – LarsH Nov 14 '19 at 13:23
  • 1
    If you're using Navigation, the back stack is in the `NavHostFragment`'s `childFragmentManager`, not the Activity's FragmentManager. – ianhanniballake Nov 14 '19 at 14:01

1 Answers1

3

It sounds like you just need to call finish() on the QuizActivity

In the fragment with your button just use activity.finish()

chrisfey
  • 388
  • 2
  • 7