我在导航图中实现了一个嵌套图,它有2个图,第一个图有3个碎片,第二个图有2个碎片。在第一个图中,有3个碎片,在第二个图中,有2个碎片。图2包含在图1中。我想导航到(图1步骤1)到(图2步骤2)。我们无法定义两个嵌套片段之间的动作。那么,有没有什么方法可以让我们将动态目的地分配给导航呢?
图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>
我查过了 这个 但这不是嵌套图的解决方案!我在导航图中实现了一个嵌套图,它有两个图。
至于 class NavGraph extends NavDestination
你可以尝试以下方法来改变嵌套图的功能。startDestination
呼叫前 navigate
到 sub_nav
:
val graph = navController.graph.findNode(R.id.sub_nav)
if (graph is NavGraph) {
graph.startDestination = R.id.createNewTaskFragment
}