4

我有一个应用程序和一个动态功能模块,我希望从中导航

应用导航图

<?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_graph"
    app:startDestination="@id/mainFragment">

    <!-- Main Fragment from App Module -->
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.xyz.MainFragment"
        android:label="MainFragment"
        tools:layout="@layout/fragment_main">
        <action
            android:id="@+id/action_mainFragment_to_nav_graph_home"
            app:destination="@id/nav_graph_home" />
    </fragment>

    <!-- Home Navigation from App Module-->
    <include app:graph="@navigation/nav_graph_home" />

    <include-dynamic
        android:id="@+id/nav_graph_dashboard"
        android:name="com.feature.dashboard"
        app:graphResName="nav_graph_dashboard"
        app:moduleName="dashboard" />

</navigation>

和功能导航

<?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_graph_dashboard"
    app:moduleName="dashboard"
    app:startDestination="@id/dashboardFragment1">

    <fragment
        android:id="@+id/dashboardFragment1"
        android:name="com.feature.DashboardFragment1"
        android:label="DashboardFragment1">
    </fragment>

</navigation>

返回错误

java.lang.IllegalStateException: The included <navigation>'s id com.xyz.dashboard:id/nav_graph_dashboard is different from the destination id com.xyz:id/nav_graph_dashboard. Either remove the <navigation> id or make them match.

似乎从功能导航中删除 id 解决了这个问题,但我找不到如何使它们匹配,即使两者都<include-dynamic>具有<navigation>相同的 idandroid:id="@+id/nav_graph_dashboard"我在这个例子中不需要 id<navigation>但我想知道何时<navigation>有一个

4

2 回答 2

6

+从您的功能导航图中的 ID 中删除:

android:id="@id/nav_graph_dashboard"

当您使用该@+id语法时,您会在动态功能的包中创建一个新 ID(您会注意到异常明确地调用每个资源 ID 的包名称正是出于这个原因)。通过删除+,您可以使用主模块包中已经创建的 ID,这使得它们匹配。

于 2020-07-02T20:46:17.113 回答
0

可以简单地实现相同的 IllegalStateException,但在根模块(通常是app模块)的根导航图中包括 app:moduleName="app"。请注意确保仅在动态功能模块导航图中设置属性。

于 2021-01-30T01:02:10.827 回答