10

Currently in my navgraph i have dialog fragment destination ( Fragment(dialog) type). When i'm navigating to this dialog and then try to navigate to another fragment from this dialog destination, dialog is closing which in my opinion is upredictable behaviour.

Now i'm only navigating to the next fragment like this.

findNavController().popBackStack(R.id.testFragment, true)

I want to dialog not closing and only navigate to another dialog like a default fragment. How can i achieve this ?

Areyana
  • 373
  • 4
  • 17
  • 1
    The fact that navigating from a dialog to another dialog automatically dismisses the previous one is the "correct" behaviour to me. Stacking dialogs on top of each other is not a good UX design in my opinion – MatPag Oct 29 '19 at 13:40

1 Answers1

11

As per this issue, this is working as intended:

There's a couple of problems here relating to how Dialogs and Fragments work:

  1. Dialogs are separate windows that always sit above your activity's window. This means that the dialog will continue to intercept the system back button no matter what state the underlying FragmentManager is in or what FragmentTransactions you do.

  2. Operations on the fragment container (i.e., your normal <fragment> destinations) don't affect dialog fragments. Same if you do FragmentTransactions on a nested FragmentManager.

The initial release of Navigation's <dialog> support did not take these limitations into account and would treat dialog destinations just like any other in that, from Navigation's point of view, they could be put on the back stack and treated like any other <fragment> destination.

As that's not actually the case, we've made a few changes for Navigation 2.1.0-alpha06 to ensure that Navigation's state of the world matches what you actually see on the screen and prevent crashes like that in comment#5.

The summary of this is that <dialog> destinations are now automatically popped when navigate to a non-dialog and non-activity destination, such as a destination. For reference, this was done in https://android-review.googlesource.com/996359 and https://android-review.googlesource.com/1007662

So it is expected when you navigate to a non-dialog destination that any dialog destination is popped off the back stack.

Community
  • 1
  • 1
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Is there any way around this? I'm using a `BottomSheetDialogFragment` and would like to be able to navigate back to it via the back button, or navigating up. – Smalls Dec 03 '20 at 20:56
  • @Smalls - no, this underlying limitation applies to all dialogs, bottom sheet or not. – ianhanniballake Dec 03 '20 at 21:07
  • Darn. We ended up needing to create a new `Activity` that has no `ActionBar`/`AppBar` and having a full flow inside of said `Activity` to solve this. We ditched the `BottomSheetDialogFragment` for a custom layout inside of a `Fragment`. The use case we were looking for is a `BottomSheetDialogFragment` pops up and clicking on something in it starts a flow of screens with no ActionBar. Users can go back to the BottomSheet. Upon completion (or backing out) of said flow we needed to take users back to where they came from initially – Smalls Dec 08 '20 at 18:14
  • @Smalls you could again show the dialog from the same fragment from which you shown it initially – Abhinav Chauhan May 31 '21 at 10:34
  • Solution is try to navigate first and then show the dialog fragment, otherwise dialog will get disappear. – takharsh Jun 16 '23 at 12:24