简体   繁体   English

是否可以在不连接 nav_graph 的情况下移动屏幕?

[英]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.从菜单 A 的屏幕到另一个屏幕的转换,而不是从菜单 A 到另一个屏幕的转换,如下所示。

menu A(fragment) -> B screen(fragment) -> C screen(fragment) -> B screen(fragment)菜单 A(片段) -> B 画面(片段) -> C 画面(片段) -> B 画面(片段)

I hooked up these screen transitions in nav_graph我在nav_graph连接了这些屏幕转换

I am using BottomNavigation .我正在使用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的屏幕到另一个屏幕的转换,而不是从菜单 A 到另一个屏幕的转换,如下所示。

menu A(fragment) -> B screen(fragment) -> C screen(fragment) -> D screen(fragment)菜单 A(片段) -> B 画面(片段) -> C 画面(片段) -> D 画面(片段)

I hooked up these screen transitions in nav_graph我在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() .但是,从DC的屏幕转换没有连接,但可以使用view.findNavController.navigate()切换屏幕。

I thought transitioning the screen was impossible without connecting to the nav_graph .我认为在不连接到nav_graph情况下转换屏幕是不可能的。

How is this possible?这怎么可能?

UPDATED更新

nav_graph.xml导航图.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>

As per the Navigate using ID documentation :根据使用 ID 文档导航

navigate(int) takes the resource ID of either an action or a destination. navigate(int)获取操作或目标的资源 ID。

So both directly navigating to any destination is possible (by using the ID of the destination) and navigating via an action is supported.因此,可以直接导航到任何目的地(通过使用目的地的 ID)并支持通过操作进行导航。

The documentation goes on to say:文档继续说:

Note: When navigating using IDs, we strongly recommend using actions where possible.注意:使用 ID 导航时,我们强烈建议尽可能使用操作。 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.通过创建操作,您可以使用Safe Args 生成的操作替换资源 ID,从而提供额外的编译时安全性。 By using an action, you can also animate transitions between the destinations.通过使用动作,您还可以为目标之间的过渡设置动画。 For more information, see Animate transitions between destinations .有关更多信息,请参阅为目标之间的过渡设置动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 是否可以有多个 nav_graph 文件? - Android Is it possible to have multiple nav_graph files? 如何在没有“全局操作”的情况下从另一个 nav_graph 导航到特定片段? - How to navigate to a specific fragment from another nav_graph without a "global action"? Robolectric 使用导航架构组件,Resources$NotFoundException: nav_graph - Robolectric using Navigation Architecture Component,Resources$NotFoundException: nav_graph nav_graph 中两个片段之间的共享视图模型 - Shared View Model between two fragments in the nav_graph 无法将默认 animation 设置为我在 nav_graph 中的操作 - Can't set default animation to my action in nav_graph 单活动架构,nav_graph 会变得这么大吗? - Single Activity Architecture, will nav_graph getting so big? 如何从第一个 nav_graph 导航到 second_graph 的特定片段? - How to navigate to specific fragment of second_graph from first nav_graph? android导航架构库(nav_graph)的fragment之间的接口回调 - Interface callback between fragments for android's navigation architecture library (nav_graph) Android:没有通过 app:startDestination for xxx:id/nav_graph 定义的起始目的地 - Android: no start destination defined via app:startDestination for x.x.x:id/nav_graph fragmentDirections.action() 方法找不到在 nav_graph 上指定的 arguments - fragmentDirections.action() method can't find arguments specified on nav_graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM