https://kotlinlang.org logo
#android
Title
# android
j

Joao Birk

09/23/2020, 2:15 PM
Hi there, I have a question about navigation components and navigating from one bottom sheet to another bottom sheet. On Bottom Sheet A I have an option that does the following:
Copy code
dismiss()
findNavController().navigate(
    R.id.action_bottom_sheet_b,
    bundleOf(...)
)
It opens the Bottom Sheet B without any problem, but if I rotate the screen I get a crash:
Copy code
java.lang.IllegalStateException
DialogFragment NUM doesn't exist in the FragmentManager
If I move the
dismiss
after the
navigate
, Bottom Sheet B doesnt show up. Seems they are in the same transaction and therefore bottom sheet B gets dismised as well. Here is my navigation.xml
Copy code
<dialog
    android:id="@+id/bottom_sheet_a"
    android:name="...BottomSheetA"
    tools:layout="@layout/dialog_bottom_sheet_a">

    <action
        android:id="@+id/action_bottom_sheet_b"
        app:destination="@id/bottom_sheet_b"/>

</dialog>
I found this on stack: https://stackoverflow.com/questions/56647476/why-dialog-does-not-have-a-navcontroller-missing And this: https://issuetracker.google.com/issues/134089818 which seems got resolved in 2.1.0-alpha06 But not sure it's the same issue. I'm using 2.3.0. If I use child
childFragmentManager.beginTransaction().show()
to show bottom sheet A instead of
findNavController().navigate()
, bottom sheet B gets displayed fine, no crash either. Any ideas of how to fix this?
google 2
i

Ian Lake

09/23/2020, 3:09 PM
dismiss()
is asynchronous and doesn't inform the
NavController
until after the
navigate()
already updates the state. You want
findNavController().popBackStack()
which does synchronously update the
NavController
instead. Or just add a
popUpTo
to your action: https://developer.android.com/guide/navigation/navigation-navigate#pop
j

Joao Birk

09/23/2020, 5:42 PM
Gotcha! Thanks for the help.
15 Views