【问题标题】:Navigate from fragment of one graph to fragment of another graph with bundle使用捆绑从一个图的片段导航到另一个图的片段
【发布时间】:2020-07-03 16:13:22
【问题描述】:

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

我想使用捆绑包从 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>

提前致谢,

【问题讨论】:

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


    【解决方案1】:

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

    然后,如果您的目标片段不是第二个图的起始片段,您需要使用 Uri,如下所示:navigate to a fragment from another graph without it being the start destination

    由于您无法使用 Uri 导航传递捆绑包,因此要传递捆绑包,您需要一种方法来设置和获取活动上的捆绑包,从第一个片段设置捆绑包并在第二个片段的 onCreate 中获取它。

    【讨论】:

    • 不,我在应用程序中使用单个活动,我有两个功能模块,每个模块都有自己的导航图
    • @Ananth 你的功能模块是动态的?
    • @Jasurbek 不,它们不是动态模块
    • @Ananth 你检查我的答案了吗?
    • 你的模块?还是外部模块?
    猜你喜欢
    • 2021-05-02
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多