android - 使用 bundle 从一个图的 fragment 导航到另一个图的 fragment

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

我有两个图表 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>

enter image description here

提前致谢

最佳答案

首先,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/

相关文章:

Android 3.0 操作栏搜索 View

android - 如何在 fragment 之间导航时消除白色闪烁/闪光?

android - 如何从 SafeArgs 中的资源中获取默认字符串值?

android - 使用 SafeArgs 导航到 Fragment 时检查参数是否存在

安卓数据库

java - 显示 R.java 资源 ID 的 TextView

android - 使用 Gradle 在 Android 项目中混合使用 Java+Scala 的最简单方法是什么?

android - 如何使用导航架构组件在另一个 Activity 上显示向上按钮

android - 无法使用 NavigationUI 设置 optionMenu

android - 添加安全参数时导航方向类未更新