我有两个图表 nav_graph1.xml 和 nav_graph2.xml(在两个不同的模块/库中)
我想从 fragment1 导航到 fragment2 with bundles。如何使用导航组件实现?
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>
提前致谢
最佳答案
首先,create a nested navGraph通过在第一个图表中包含第二个图表。
然后,如果您的目标 fragment 不是第二个图的开始 fragment ,您需要使用 Uri,如此处所示:navigate to a fragment from another graph without it being the start destination
由于您无法通过 Uri 导航传递 bundle ,因此要传递 bundle ,您需要一种方法来设置和获取 Activity 中的 bundle ,从第一个 fragment 设置 bundle 并在第二个 fragment 的 onCreate 中获取它。
关于android - 使用 bundle 从一个图的 fragment 导航到另一个图的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62719174/