'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 1:[1]
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)
Solution 2:[2]
Your home module graph has:
<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"
So the ID of your home graph is @id/home_navigation but that's not the ID you've set as the startDestination of your app level graph, hence the error message:
navigation destination com.example.multimodulepoc:id/navigation_home is not a direct child of this NavGraph
The 'direct child' is the most important part here: your app level graph has no knowledge of the destinations within your home graph, just the graph itself. Therefore, you need to ensure that the startDestination of your app level graph actually matches the android:id of your home graph:
<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/home_navigation">
<include app:graph="@navigation/home_navigation" />
<include app:graph="@navigation/dashboard_navigation" />
</navigation>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kashyap |
| Solution 2 | ianhanniballake |
