我的项目使用 SafeArgs。我已经切换到其他人创建的分支,并且构建项目会生成编译器错误,因为即使在 nav_graph 中,生成的“~Directions”类方法也会返回 ActionOnlyNavDirections(没有传递给目标片段的参数)接受一个论点。
例如,使用以下 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>
我结束了以下方向类:
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)
}
}
和:
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)
}
}
如您所见,FragmentC 采用“userName”参数,而 actionFragmentAToFragmentC 尊重这一点,而 actionFragmentBToFragmentC 则没有。 我尝试过清理、手动删除构建文件夹、使缓存无效并重新启动以及重新构建,但生成的类仍然看起来总是一样的。为什么 SafeArgs 为一个方向类而不是另一个生成参数?
如何在构建时调试 SafeArgs 插件以了解更多关于导致此问题的原因?