0

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>
ybybyb
  • 1,385
  • 1
  • 12
  • 33

1 Answers1

2

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • `we strongly recommend using actions where possible` means to put the `ID` of the `action tag` in `navigate()`, not the `ID` of the `fragment`? Or do you mean use [this](https://developer.android.com/guide/navigation/navigation-navigate#example-safeargs)? – ybybyb Jul 02 '21 at 17:40
  • 1
    Yes, if you're using the `navigate(int)` version, you should use the ID of an action. Safe Args is better still – ianhanniballake Jul 02 '21 at 18:02
  • Thanks. If so, I have a question for your answer. When using `navigate(int)` instead of `safe args action`, why use the `action tag's ID` instead of the `fragment ID`? As a result of my experiments, there was no problem with screen switching. – ybybyb Jul 02 '21 at 18:14
  • oh sorry. Is the part I asked the question already in the part you answered? (`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. `) – ybybyb Jul 02 '21 at 18:16
  • 1
    Right, the visual representation and the ability to swap out IDs entirely for Safe Args generated code is what you'd gain by defining actions that only use `app:destination`, like your example does. – ianhanniballake Jul 02 '21 at 18:36
  • In summary, when using `navigate(int)`, both`resource ID` and `action tag ID` are possible, but it is better to use `action ID`, but the best method is to use `Safe Args` created by defining `action`. Is that right? – ybybyb Jul 02 '21 at 18:44
  • Thank you so much. Thanks for always helping – ybybyb Jul 03 '21 at 20:16