【问题标题】:android navigation components passing data between fragment directionsandroid导航组件在片段方向之间传递数据
【发布时间】:2020-07-06 08:20:56
【问题描述】:

这是我的导航文件

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

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.example.fragmentdatapassings.FragmentA"
        android:label="Fragment A">
        <action
            android:id="@+id/action_fragmentA_to_fragnentB"
            app:destination="@+id/fragmentB" />
        <argument
        android:name="x"
        app:argType="integer" />
    </fragment>

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.fragmentdatapassings.FragmentB"
        android:label="FragmentB">
    </fragment>

</navigation>

这是片段方向数据传递的代码。

val x : Int = 10
val action = FragmentADirections.actionFragmentAToFragnentB(x)
Navigation.findNavController(requireView()).navigate(action)

上面第二行显示错误

公开娱乐的争论太多

当我将鼠标悬停在错误上时,它显示添加参数 x,我确实单击它并且它确实 在 FragmentADirections 中添加参数 x。但是再次构建应用程序后,它显示错误 再次。

【问题讨论】:

标签: android kotlin navigation fragment


【解决方案1】:

&lt;argument&gt; 应该在您想要接收的片段中,而不是您发送的片段中

如果你想将数据从片段 A 传递到 B

  <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.fragmentdatapassings.FragmentB"
        android:label="FragmentB">
        <argument
        android:name="x"
        app:argType="integer" />
    </fragment>

并从片段 A 中删除参数,它应该没问题

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2015-08-21
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多