androidandroid-fragmentsandroid-bottomnavigationview

Is it possible to move the screen without connecting in nav_graph?


I am using BottomNavigation. The transition from the screen of menu A to another screen, not the screen transition from menu A to another, is as follows.

menu A(fragment) -> B screen(fragment) -> C screen(fragment) -> B screen(fragment)

I hooked up these screen transitions in nav_graph

I am using BottomNavigation. The transition from the screen of menu A to another screen, not the screen transition from menu A to another, is as follows.

menu A(fragment) -> B screen(fragment) -> C screen(fragment) -> D screen(fragment)

I hooked up these screen transitions in nav_graph

However, the screen transition from D to C was not connected, but it was possible to switch the screen using view.findNavController.navigate().

I thought transitioning the screen was impossible without connecting to the nav_graph.

How is this possible?

UPDATED

nav_graph.xml

<?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/nav_graph"
    app:startDestination="@id/calendar">
    <fragment
        android:id="@+id/calendar"
        android:name="com.example.writeweight.fragment.CalendarFragment"
        android:label="fragment_calendar"
        tools:layout="@layout/fragment_calendar" >
    </fragment>
    <fragment
        android:id="@+id/list"
        android:name="com.example.writeweight.fragment.WorkoutListFragment"
        android:label="fragment_workout_list"
        tools:layout="@layout/fragment_workout_list" />
    <!-- menu A fragment -->
    <fragment
        android:id="@+id/write_home"
        android:name="com.example.writeweight.fragment.WriteRoutineHomeFragment"
        android:label="fragment_write_routine_home"
        tools:layout="@layout/fragment_write_routine_home" >
        <action
            android:id="@+id/action_write_home_to_bodyPartDialog"
            app:destination="@id/bodyPartDialog" />
    </fragment>
    
    <!-- B screen -->
    <dialog
        android:id="@+id/bodyPartDialog"
        android:name="com.example.writeweight.fragment.BodyPartDialogFragment"
        android:label="BodyPartDialogFragment"
        tools:layout="@layout/fragment_body_part_dialog">
        <action
            android:id="@+id/action_bodyPartDialog_to_write"
            app:destination="@id/write"/>
    </dialog>
    
    <!-- C screen -->
    <fragment
        android:id="@+id/write"
        android:name="com.example.writeweight.fragment.WritingRoutineFragment"
        android:label="WritingRoutineFragment"
        tools:layout="@layout/fragment_writing_routine">
        <action
            android:id="@+id/action_write_to_bodyPartDialog"
            app:destination="@id/bodyPartDialog" />
        <argument
            android:name="title"
            app:argType="string"
            android:defaultValue="" />
    </fragment>
    
    <!-- D screen   -->
    <fragment
        android:id="@+id/workoutListTabFragment"
        android:name="com.example.writeweight.fragment.WorkoutListTabFragment"
        android:label="fragment_workout_list_tab"
        tools:layout="@layout/fragment_workout_list_tab" />
</navigation>

Solution

  • As per the Navigate using ID documentation:

    navigate(int) takes the resource ID of either an action or a destination.

    So both directly navigating to any destination is possible (by using the ID of the destination) and navigating via an action is supported.

    The documentation goes on to say:

    Note: When navigating using IDs, we strongly recommend using actions where possible. Actions provide additional information in your navigation graph, visually showing how your destinations connect to each other. By creating actions, you can replace resource IDs with Safe Args-generated operations, providing additional compile-time safety. By using an action, you can also animate transitions between the destinations. For more information, see Animate transitions between destinations.