GaRRaPeTa
GaRRaPeTa

Reputation: 5598

Android Navigation Component - external included graph receiving one argument

I am using the navigation component from Jetpack.

My main graph is huge, so I have decided to split it into different graphs and use the <include /> functionality.

Problem is that some of the included graphs receive arguments, but Android seems to ignore the arguments when creating the navigation Directions

Ie:

Main Graph (simplified)

<?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">
    <fragment
        android:id="@+id/listFragment"
         android:name="com......ListFragment">
        <action
            android:id="@+id/view_detail"
            app:destination="@id/item_detail"/>
    </fragment>

    <include app:graph="@navigation/included_graph"/>

</navigation>

Included graph: (simplified)

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_detail"
    app:startDestination="@id/itemDetailFragment">
    <argument
        android:name="item_id"
        app:argType="integer" />
    <fragment
        android:id="@+id/itemDetailFragment"
        android:name="com......ItemDetailFragment">

        <argument
            android:name="item_id"
            app:argType="integer" />
    </fragment>
    [...]
</navigation>

The class generated by android is

class ListFragmentDirections private constructor() {
    companion object {
        fun viewDetail(): NavDirections =
                ActionOnlyNavDirections(R.id.view_detail)
    }
}

It seems the argument received by the included graph is ignored - I can't pass any argument safely.

Is there are workaround for this?

I'm interested in any alternative letting me breaking down the graph into smaller files, based on <include /> or not.

Upvotes: 3

Views: 968

Answers (1)

Alex Timonin
Alex Timonin

Reputation: 1988

There is a workaround: include the argument to the action explicitly, i.e.

<?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">
    <fragment
        android:id="@+id/listFragment"
         android:name="com......ListFragment">
        <action
            android:id="@+id/view_detail"
            app:destination="@id/item_detail">
            <argument
                android:name="item_id"
                app:argType="integer" />
        </action>
    </fragment>

    <include app:graph="@navigation/included_graph"/>

</navigation>

Upvotes: 2

Related Questions