Navigating to other graph part in navigation components

Viewed 71

I have the problem that my sample app crashes when I deeplink from one graph part to the other one.

My structure is simple:
Two tabs with own graphs which have each two possible fragments

When I navigate to a detail page (in my sample a book) this just works from my "Shelf" but not from the home screen.

Here is my full 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/sample"
    app:startDestination="@id/tab_home">

    <navigation
        android:id="@+id/tab_home"
        app:startDestination="@id/frag_home">
        <fragment
            android:id="@+id/frag_home"
            android:name="eu.rekisoft.android.navbug.HomeFragment"
            tools:layout="@layout/fragment_home">
            <action
                android:id="@+id/cause_bug"
                app:destination="@id/frag_hint"/>
            <action
                android:id="@+id/causeCrash"
                app:destination="@id/frag_book">
                <argument
                    android:name="name"
                    app:argType="string"/>
            </action>
        </fragment>

        <fragment
            android:id="@+id/frag_hint"
            android:name="eu.rekisoft.android.navbug.HintFragment"
            android:label="Hint"
            tools:layout="@layout/fragment_hint"/>
    </navigation>

    <navigation
        android:id="@+id/tab_books"
        app:startDestination="@id/frag_shelf">
        <fragment
            android:id="@+id/frag_shelf"
            android:name="eu.rekisoft.android.navbug.ShelfFragment"
            android:label="Shelf Fragment"
            tools:layout="@layout/fragment_shelf">
            <action
                android:id="@+id/open_book"
                app:destination="@id/frag_book">
                <argument
                    android:name="name"
                    app:argType="string"/>
            </action>
        </fragment>

        <fragment
            android:id="@+id/frag_book"
            android:name="eu.rekisoft.android.navbug.BookFragment"
            android:label="Book Fragment"
            tools:layout="@layout/fragment_book">
            <argument
                android:name="name"
                app:argType="string"/>
        </fragment>
    </navigation>
</navigation>

Here is my crash:

java.lang.IllegalArgumentException: Navigation destination eu.rekisoft.android.navbug:id/frag_book referenced from action eu.rekisoft.android.navbug:id/causeCrash cannot be found from the current destination Destination(eu.rekisoft.android.navbug:id/frag_home) class=eu.rekisoft.android.navbug.HomeFragment
    at androidx.navigation.NavController.navigate(NavController.kt:1527)
    at androidx.navigation.NavController.navigate(NavController.kt:1464)
    at androidx.navigation.NavController.navigate(NavController.kt:1922)
    at eu.rekisoft.android.navbug.HomeFragment.onViewCreated$lambda-2$lambda-1(HomeFragment.kt:23)
    at eu.rekisoft.android.navbug.HomeFragment.$r8$lambda$q53N5QXeJ-lbbpd7MjwkcKbt9_4(Unknown Source:0)
    at eu.rekisoft.android.navbug.HomeFragment$$ExternalSyntheticLambda1.onClick(Unknown Source:2)

Very clear error message, but I don't know how to fix this.

If you want to checkout my code check this GitHub repo.

0 Answers
Related