public final fun actionFragmentAToDetailActivity() 参数太多

Too many arguments for public final fun actionFragmentAToDetailActivity()

实际上,库模块中存在 DetailActivity(需要传递参数),我需要从应用程序 (FragmentA) 启动它,但我收到构建错误如下:

Too many arguments for public final fun actionFragmentAToDetailActivity()

我正在使用 SafeArgs 库来传递数据。

片段A

 findNavController().navigate(
            FragmentADirections.actionFragmentAToDetailActivity(
                "id",
                "name"
            )
        )

nav_graph_app.xml(出现在应用程序模块中)

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

<fragment
    android:id="@+id/fragmentA"
    android:name="com.abhishek.fragmentA"
    android:label="WeeklyWorkListFragment"
    tools:layout="@layout/fragment_a">
    
    <action
        android:id="@+id/action_fragmentA_to_DetailActivity"
        app:destination="@id/detail_activity" />
</fragment>
</navigation>

nav_graph_common_component.xml(存在于库项目中,作为库模块包含在应用程序的 build.gradle 中)

<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/common_component_navigation"

<activity
    android:id="@+id/detail_activity"
    android:name="com.components.DetailActivity"
    android:label="DetailActivity"
    tools:layout="@layout/activity_detail">
    <argument
        android:name="id"
        app:argType="string" />
    <argument
        android:name="name"
        app:argType="string" />
</activity>
</navigation>

我们将不胜感激任何帮助或指导。

片段A

findNavController().navigate(
                FragmentADirections.actionFragmentAToDetailActivity(
                    "id"
                ).setname("name")
            )

1.Try 将 bundle 与 seriable

结合使用

2.

val bundle = Bundle().apply {
                putSerializable("article", it)
            }
            findNavController().navigate(
                R.id.action_savedNewsFragment_to_articleNewsFragment,
                bundle
            )
  1. 在你的片段中

val args:ArticleNewsFragmentArgs by navArgs()

您只需添加:

<include app:graph="@navigation/nav_graph_common_component" />

在你的nav_graph_app.xml