Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SafeArgs generated action is missing arguments

My project uses SafeArgs. I've switched to a branch that someone else created, and building the project generates a compiler error, because the generated "~Directions" class' methods for return ActionOnlyNavDirections (no arguments passed to destination fragment) even though in the nav_graph, the fragment takes an argument.

For example, with the following nav_graph.xml:

<fragment
    android:id="@+id/fragment_a"
    android:name="com.myapp.ui.fragment.FragmentA"
    android:label="Fragment A"
    tools:layout="@layout/fragment_a">

<argument
        android:name="userName"
        android:defaultValue=" "
        app:argType="string" />
    <action
        android:id="@+id/action_fragment_a_to_fragmentX"
        app:destination="@id/fragmentX" />

    <action
        android:id="@+id/action_fragment_a_to_homeFragment"
        app:destination="@id/homeFragment" />

    <action
        android:id="@+id/action_fragmentA_to_fragmentC"
        app:destination="@id/fragmentC"
        app:popUpTo="@id/loginFragment" />

</fragment>

<fragment
    android:id="@+id/fragment_b"
    android:name="com.myapp.ui.fragment.FragmentB"
    android:label="Fragment B"
    tools:layout="@layout/fragment_b">

    <argument
        android:name="from"
        app:argType="com.myapp.data.local.model.ToFragmentBFrom"/>

    <action
        android:id="@+id/action_fragmentB_to_homeFragment"
        app:destination="@id/homeFragment" />

    <action
        android:id="@+id/action_fragmentB_to_fragmentC"
        app:destination="@id/fragmentC"
        app:popUpTo="@id/homeFragment" />

</fragment>

<fragment
    android:id="@+id/fragment_c"
    android:name="com.myapp.fragment.fragmentC"
    android:label="Fragment C"
    tools:layout="@layout/fragment_c">

    <argument
        android:name="userName"
        app:argType="string" />
</fragment>

I wind up with the following Directions classes:

class FragmentADirections private constructor() {
    private data class ActionFragmentAToFragmentC(val userName: String) : NavDirections {
        override fun getActionId(): Int = R.id.action_fragmentA_to_fragmentC

        override fun getArguments(): Bundle {
            val result = Bundle()
            result.putString("userName", this.userName)
            return result
        }
    }

    companion object {
        fun actionFragmentAToFragmentX(): NavDirections =
                ActionOnlyNavDirections(R.id.action_fragmentA_to_fragmentX)

        fun actionFragmentAToHomeFragment(): NavDirections =
                ActionOnlyNavDirections(R.id.action_fragmentA_to_homeFragment)

        fun actionFragmentAToFragmentC(userName: String): NavDirections =
                ActionFragmentAToFragmentC(userName)

        fun actionGlobalFragmentA(userName: String = " "): NavDirections =
                NavGraphDirections.actionGlobalFragmentA(userName)


    }
}

and:

class FragmentBDirections private constructor() {
    companion object {
        fun actionFragmentBToHomeFragment(): NavDirections =
                ActionOnlyNavDirections(R.id.action_fragmentA_to_homeFragment)

        fun actionFragmentBToFragmentC(): NavDirections =
                ActionOnlyNavDirections(R.id.action_fragmentB_to_fragmentC)
    }
}

AS you can see, FragmentC takes a "userName" argument, and the actionFragmentAToFragmentC respects this, whereas actionFragmentBToFragmentC does not. I've tried cleaning, manually deleting the build folder, invalidating cache and restarting, and rebuilding but still the generated classes always look the same. Why is SafeArgs generating arguments for one Directions class and not the other?

How can I debug the SafeArgs plugin at build time to learn more about what's causing this?

like image 819
duggulous Avatar asked Mar 17 '26 06:03

duggulous


1 Answers

I changed from "safeargs" to "safeargs.kotlin" and it worked. Although the Docs said "safeargs" should work on both Java and Kotlin.

plugins {
  // id 'androidx.navigation.safeargs' 
  id 'androidx.navigation.safeargs.kotlin'
}
like image 58
Rocky Avatar answered Mar 18 '26 20:03

Rocky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!