是否可以在嵌套导航图 Android 中拥有动态目的地?

Mad*_*ddy 15 android android-navigation android-jetpack

我在导航图中实现了一个嵌套图,它有 2 个图。在第一个图中,有 3 个片段,在第二个图中,有 2 个片段。图 2 包含在图 1 中。我想导航到(图 1 第 1 步)到(图 2 第 2 步)。我们不能在两个嵌套片段之间定义动作。那么有什么方法可以将动态目的地分配给导航吗?

在此处输入图片说明

图一

<?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/mobile_navigation"
    app:startDestination="@id/dashboardFragment">
    <fragment
        android:id="@+id/dashboardFragment"
        android:name="com.navigationgraphexample.fragments.DashboardFragment"
        android:label="fragment_dashboard"
        tools:layout="@layout/fragment_dashboard" >
        <action
            android:id="@+id/action_dashboardFragment_to_stepOneFragment2"
            app:destination="@id/stepOneFragment" />
        <action
            android:id="@+id/action_dashboardFragment_to_sub_nav"
            app:destination="@id/sub_nav" />

    </fragment>
    <fragment
        android:id="@+id/stepOneFragment"
        android:name="com.navigationgraphexample.fragments.StepOneFragment"
        android:label="fragment_step_one"
        tools:layout="@layout/fragment_step_one">
        <action
            android:id="@+id/action_stepOneFragment_to_stepTwoFragment"
            app:destination="@id/stepTwoFragment" />
    </fragment>
    <fragment
        android:id="@+id/stepTwoFragment"
        android:name="com.navigationgraphexample.fragments.StepTwoFragment"
        android:label="fragment_step_two"
        tools:layout="@layout/fragment_step_two" />
    <fragment
        android:id="@+id/notificationFragment"
        android:name="com.navigationgraphexample.fragments.NotificationFragment"
        android:label="fragment_notification"
        tools:layout="@layout/fragment_notification" >
        <deepLink
            android:id="@+id/deepLink"
            app:uri="myapp.com/{myargs}"
            />
        <argument android:name="myargs"
            app:argType="integer"
            android:defaultValue="1" />
    </fragment>
    <include app:graph="@navigation/sub_nav" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

图2

<?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/sub_nav"
    app:startDestination="@id/createNewTaskFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="com.navigationgraphexample.fragments.ListFragment"
        android:label="fragment_list"
        tools:layout="@layout/fragment_list" />
    <fragment
        android:id="@+id/createNewTaskFragment"
        android:name="com.navigationgraphexample.fragments.CreateNewTaskFragment"
        android:label="fragment_create_new_task"
        tools:layout="@layout/fragment_create_new_task" >
        <action
            android:id="@+id/action_createNewTaskFragment_to_listFragment"
            app:destination="@id/listFragment" />
    </fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)

我已经检查了这个解决方案,但它不适用于嵌套图!

Vas*_*lov 10

至于class NavGraph extends NavDestination,你可以试试下面的方法修改嵌套图的startDestination调用之前navigatesub_nav

val graph = navController.graph.findNode(R.id.sub_nav)
if (graph is NavGraph) {
    graph.startDestination = R.id.createNewTaskFragment
}
Run Code Online (Sandbox Code Playgroud)

  • 可以使用它来查找当前目标嵌套图:findNavController().currentDestination?.parent?.findNode(R.id.nestedGraph) (2认同)