Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Architecture Component - DestinationFragmentArgs is not generated

I have this in app gradle:

apply plugin: 'androidx.navigation.safeargs'

implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0'

and this in the project gradle:

classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0'

the navigation 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/navigation_graph"
    app:startDestination="@id/loginPhoneNumberFragment">

    <fragment
        android:id="@+id/loginPhoneNumberFragment"
        android:name="...fragments.LoginPhoneNumberFragment"
        android:label="@string/login_activity_title"
        tools:layout="@layout/fragment_login_phone_number">
        <action
            android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
            app:destination="@id/loginCodeFragment">
            <argument
                android:name="prefix"
                app:argType="string" />
            <argument
                android:name="phone_number"
                app:argType="string" />
        </action>
    </fragment>

    <fragment
        android:id="@+id/loginCodeFragment"
        android:name="...LoginCodeFragment"
        android:label="@string/login_activity_title"
        tools:layout="@layout/fragment_login_code" />

</navigation>

LoginPhoneNumberFragment:

val action = LoginPhoneNumberFragmentDirections.actionLoginPhoneNumberFragmentToLoginCodeFragment(prefix, phoneNumber)
view?.findNavController()?.navigate(action)

LoginCodeFragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val prefix = LoginCodeFragmentArgs.fromBundle(arguments).prefix //LoginCodeFragmentArgs is not recognized

    }

In the LoginPhoneNumberFragment it creates the "LoginPhoneNumberFragmentDirections" class, but on the destination class, LoginCodeFragment, it doesn't recognize "LoginCodeFragmentArgs".

Can someone please tell me what am I missing? (I cleaned and rebuilded, and tried Invalidate caches...)

like image 581
MorZa Avatar asked May 29 '19 12:05

MorZa


2 Answers

Ok, so after a lot of searching I found out my mistake - the arguments should be on the Destination fragment, and not on the starting one:

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

    <fragment
        android:id="@+id/loginPhoneNumberFragment"
        android:name="...fragments.LoginPhoneNumberFragment"
        android:label="@string/login_activity_title"
        tools:layout="@layout/fragment_login_phone_number">
        <action
            android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
            app:destination="@id/loginCodeFragment">
        </action>
    </fragment>

    <fragment
        android:id="@+id/loginCodeFragment"
        android:name="...fragments.LoginCodeFragment"
        android:label="@string/login_activity_title"
        tools:layout="@layout/fragment_login_code" >
        <argument
            android:name="prefix"
            app:argType="string"
            android:defaultValue="888" />
        <argument
            android:name="phone_number"
            app:argType="string"
            android:defaultValue="88888888"/>
    </fragment>

</navigation>

You can also add it manually via the navigation graph design - press on the destination fragment and press "+" on the arguments section, it will add it to the text file.

like image 157
MorZa Avatar answered Sep 21 '22 16:09

MorZa


The argument should be in the destination fragment as follows instead of inside the action in source fragment

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

    <fragment
        android:id="@+id/loginPhoneNumberFragment"
        android:name="...fragments.LoginPhoneNumberFragment"
        android:label="@string/login_activity_title"
        tools:layout="@layout/fragment_login_phone_number">
        <action
            android:id="@+id/action_loginPhoneNumberFragment_to_loginCodeFragment"
            app:destination="@id/loginCodeFragment"/>
    </fragment>

    <fragment
        android:id="@+id/loginCodeFragment"
        android:name="...LoginCodeFragment"
        android:label="@string/login_activity_title"
        tools:layout="@layout/fragment_login_code">
            <argument
                android:name="prefix"
                app:argType="string" />
            <argument
                android:name="phone_number"
                app:argType="string" />
     </fragment>
</navigation>
like image 34
Vinil Chandran Avatar answered Sep 19 '22 16:09

Vinil Chandran