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

时间:2018-09-14 07:08:30

标签: android android-navigation android-jetpack

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

enter image description here

  

图1

<?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>
  

图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>

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

1 个答案:

答案 0 :(得分:4)

class NavGraph extends NavDestination而言,您可以在调用startDestinationnavigate之前尝试以下方法来更改嵌套图的sub_nav

val graph = navController.graph.findNode(R.id.sub_nav)
if (graph is NavGraph) {
    graph.startDestination = R.id.createNewTaskFragment
}