4

I have one app and one dynamic feature module i wish to navigate from

App nav graph

<?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>

And feature 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>

Returns error

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.

It seems that removing id from feature navigation solves the issue but i couldn't find how to make them match even though both <include-dynamic> and <navigation> have the same id android:id="@+id/nav_graph_dashboard" I don't need id for <navigation> for this example but i wonder if it's possible when <navigation> has one

Thracian
  • 43,021
  • 16
  • 133
  • 222

2 Answers2

8

Remove the + from your ID in your feature navigation graph:

android:id="@id/nav_graph_dashboard"

When you use the @+id syntax, you create a new ID in your dynamic feature's package (you'll note the exception explicitly calls out the package names for each resource ID for exactly that reason). By removing the +, you use the already created ID from the main module's package, which makes them match.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • It seems the ids matching across include-dynamic and the dynamic-feature module's nav graph's navigation tag is not important - it is not the mechanism used to make the connection, rather graphResName is. The graphResName should be the filename of the dynamic-feature module's nav_graph. The above will be clear when dealing with multiple uses of the destination Fragment across multiple dynamic-feature module nav graphs. – straya Jan 30 '21 at 01:37
  • ...and if defining the destination Fragment (with appModule attr) in root nav graph and navigating using that tag's id: graphResName is then depended upon for the resolution of the target nav graph. – straya Jan 30 '21 at 01:44
0

The same IllegalStateException can be achieved simply but including app:moduleName="app" in the root nav graph in the root module (usually app module). Take care to ensure that attribute is only set in dynamic feature module nav graphs.

straya
  • 5,002
  • 1
  • 28
  • 35