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
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With