使用捆绑包从一个图的片段导航到另一个图的片段

时间:2020-07-03 16:13:22

标签: android android-architecture-navigation android-safe-args

我有两个图nav_graph1.xml和nav_graph2.xml(在两个不同的模块/ lib中)

我想从
bundle 从fragment1导航到fragment2。如何使用导航组件?

nav_graph1.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_graph1"
    app:startDestination="@id/fragment1">
    <fragment
        android:id="@+id/C1_fragment"
        android:name="com.example.multiplenavigation.C_nav_graph.C1 "
        android:label="C1"
        tools:layout="@layout/fragment_C1">
        <action
            android:id="@+id/C1_to_C2_fragment"
            app:destination="@id/C2_fragment" />
    </fragment>
</navigation>

nav_graph2.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_graph1"
    app:startDestination="@id/fragment2">
    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.multiplenavigation.C_nav_graph.C1 "
        android:label="C2"
        tools:layout="@layout/fragment_C2">
    </fragment>
</navigation>

enter image description here

预先感谢

1 个答案:

答案 0 :(得分:0)

首先,create a nested navGraph,将第二张图包括在第一张图中。

然后,如果目标片段不是第二张图的开始片段,则需要使用Uri,如以下答案中所示:navigate to a fragment from another graph without it being the start destination

由于您无法通过Uri导航传递捆绑软件,因此要传递捆绑软件,您需要某种方法来设置和获取活动中的捆绑软件,从第一个片段设置捆绑软件并将其放入第二个片段的onCreate中。