Search code examples
androidnavigationandroid-jetpack-navigation

Navigation destination com.example.multimodulepoc:id/navigation_home is not a direct child of this NavGraph


I'm trying to add navigation of multiple modules in the app using tag I've provided my startDestination from home module. Its working fine when I've only one <include> tag with startDestination. App getting crash with following error message when I added multiple module navigations. I found no useful links in SO. Please help us to resolve this issue. Thanks in advance.

Caused by: java.lang.IllegalArgumentException: navigation destination com.example.multimodulepoc:id/navigation_home is not a direct child of this NavGraph 

Below is my navigation xml and dependency.

app build.gradle.

dependencies {
    ....
    implementation project( ':home')
    implementation project( ':Dashobard')
    ....
}

App level Navigation xml.

<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/mobile_navigation"
    app:startDestination="@id/navigation_home">

    <include app:graph="@navigation/home_navigation" />
    <include app:graph="@navigation/dashboard_navigation" />

</navigation>

Home module 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/home_navigation"
    app:startDestination="@+id/navigation_home">
    <fragment
        android:id="@+id/navigation_home"
        android:name="com.ns.mpos.home.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />
</navigation>

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/dashboard_navigation"
    app:startDestination="@+id/navigation_dashboard">

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.ns.mpos.dashobard.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

</navigation>

Solution

  • I'faced similar crash if trying to navigate another module navigation.

    java.lang.IllegalArgumentException: Navigation action/destination com.example.multimodulepoc:id/navigation_dashboard cannot be found from the current destination Destination(com.example.multimodulepoc:id/navigation_home) label=Home class=com.example.home.home.HomeFragment 
    

    With the help of @ianhanniballake comments, Fixed the crash with following code helps me to resolve the crash.

    findNavController().setGraph(R.navigation.dashboard_navigation)
    findNavController().navigate(R.id.navigation_dashboard)