Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationComponent navigation within a BottomSheetDialogFragment

Can Android's Navigation Component be used for navigation WITHIN a BottomSheet (i.e. replacing/adding fragments within a single bottom sheet)?

I know how to launch a BottomSheetDialogFragment using the <dialog> tag within a Navigation Graph. For example, the below nav_graph.xml allows the user to navigate from one BottomSheetDialogFragment (fragmentOne) to another BottomSheetDialogFragment (fragmentTwo). FragmentTwo opens as a second BottomSheet over FragmentOne's BottomSheet.

However, what if I wanted fragmentTwo to replace fragmentOne WITHIN THE SAME BottomSheet? How would I accomplish this using the Navigation Graph?

<navigation android:id="@+id/nav_graph"
        app:startDestination="@id/fragmentOne">

    <dialog android:id="@+id/fragmentOne"
        android:name="com.example.navcomponentapp.FragmentOne"
        android:label="fragment_fragment_one"
        tools:layout="@layout/fragment_fragment_one">

        <action android:id="@+id/action_fragmentOne_to_fragmentTwo2"
            app:destination="@id/fragmentTwo"/>
    </dialog>

    <dialog android:id="@+id/fragmentTwo"
        android:name="com.example.navcomponentapp.FragmentTwo"
        android:label="fragment_fragment_two"
        tools:layout="@layout/fragment_fragment_two"/>
</navigation>

Demo (this is not what I want. I don't want a bottomSheet opening over another bottomSheet

like image 911
VIN Avatar asked Jan 27 '20 01:01

VIN


People also ask

What is navigation architecture component?

Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.


1 Answers

A NavHostFragment is the container that gets its contents replaced. Therefore, if you want to have a container within your BottomSheetDialogFragment that is separate from the outer NavHostFragment's container (i.e., the container that is ~the whole content of your activity), you'd want to add a separate NavHostFragment within the layout of your BottomSheetDialogFragment with its own navigation graph.

Then navigating within that smaller container would only replace the contents within the bottom sheet (and you'd use requireParentFragment().findNavController() to access the outer NavController if you wanted to do a navigate() operation at the outer level).

like image 126
ianhanniballake Avatar answered Oct 01 '22 04:10

ianhanniballake