Android SafeArgs 生成的操作缺少参数

dug*_*ous 9 android android-architecture-navigation android-safe-args

我的项目使用 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>
Run Code Online (Sandbox Code Playgroud)

我结束了以下方向类:

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)


    }
}
Run Code Online (Sandbox Code Playgroud)

和:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,FragmentC 采用“userName”参数,并且 actionFragmentAToFragmentC 尊重这一点,而 actionFragmentBToFragmentC 则不然。 我试过清理、手动删除构建文件夹、使缓存无效并重新启动和重建,但生成的类仍然看起来总是一样的。为什么 SafeArgs 为一个 Directions 类而不是另一个类生成参数?

如何在构建时调试 SafeArgs 插件以了解有关导致此问题的更多信息?

goe*_*mic 2

这听起来和感觉有点尴尬,因为生成的FragmentBDirections.class应该自动包含FragmentC的参数。有时我也会遇到这个问题,但我无法告诉你为什么 safe args 插件会这样,特别是为什么它的行为并不总是相似的。

要解决此问题,请<argument><action>.

<action
    android:id="@+id/action_fragmentB_to_fragmentC"
    app:destination="@id/fragmentC"
    app:popUpTo="@id/homeFragment" />
    
    <argument
        android:name="userName"
        app:argType="String" />
</action>
Run Code Online (Sandbox Code Playgroud)

当然,这会在 NavGraph 中产生一些代码开销,因为您需要始终在<fragment>和 中指定参数<action>。这并不是“最干净”的解决方案。但这对我来说总是有效,我仍然可以从 SafeArgs 中受益。