46

I am using BottomSheetDialogFragment for displaying few custom setting's.

Requirement:

When i click on any tab in BottomSheetDialogFragment i replace the fragment and add it to backstack so that when user click's onBackPress or Up action it should go back the the last setting's fragment of BottomSheetDialogFragment.

I want to use Navigation Architecture Component to simplify my transaction's.

Issue: if i use Navigation Architecture Component to navigate from FragmentA to BottomSheetDialogFragment then i receive below error.

java.lang.IllegalStateException: dialog must not be null BottomSheetDialogFragment

I don't know how to instantiate BottomSheetDialogFragment using Navigation Architecture Component and and using below code will not have a maintain backstack using Navigation Architecture Component.

BottomSheetDialogFragment.show(FragmentManager manager, String tag)
Anmol
  • 8,110
  • 9
  • 38
  • 63

1 Answers1

93

In the navigation component version 2.1.0-alpha04, Navigation Graph can contain dialog as one of the destinations.

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_navigation"
    app:startDestination="@id/startFragment">

    <fragment
        android:id="@+id/loginFragment"
        android:name="com.awesomeproject.android.authentication.login.LoginFragment"
        android:label="Login"
        tools:layout="@layout/login_fragment" />

    <dialog
        android:id="@+id/bottomSheet"
        android:name="com.awesomproject.android.BottomSheetFragment"
        tools:layout="@layout/bottom_sheet_dialog_fragment" />

</navigation>

The BottomSheetFragment will look similar to other BottomSheet.

class BottomSheetFragment : BottomSheetDialogFragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View =
            inflater.inflate(R.layout.bottom_sheet_dialog_fragment, container, false)
}

Then you can treat bottomSheet the same way as other destinations. You can navigate to this destination or passing safeArgs in.

Cheers!

Boonya Kitpitak
  • 3,607
  • 1
  • 30
  • 30
  • 4
    I tried this but when i try to navigate from dialog using navigation then NavController is missing can you share code of navigation from dialog to other fragment's or Dialog's? @Boonya – Anmol Jun 18 '19 at 11:02
  • 3
    with this solution, the bottom sheet is being displayed in another fragment does not shadow the current fragment that call it – AndroLife Sep 29 '19 at 00:02
  • 3
    Oh man, it was scary when I added BottomSheet with navigation when it was opened in full screen. But you save my time. It was easy to just change dialog from a fragment. – Pranav P Jan 08 '20 at 05:01
  • @AndroLife do you happen to know the solution for that issue? – Ralph Jan 21 '21 at 14:45
  • @AndroLife FWIW I'm seeing correct behavior with material 1.4.0 and navigation 2.3.5 – tir38 Sep 25 '21 at 16:58
  • @tir38 I get an issue with the navigation controller when I need to open 2 bottom sheets from the same fragment. One after another. – pratz9999 Oct 29 '21 at 06:06
  • @tir38 Did you have an answer for this situation ? or used legacy approach ? – NimaAzhd Feb 08 '22 at 12:55