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?