【问题标题】:How i can add arguments when i navigate to another fragment?当我导航到另一个片段时如何添加参数?
【发布时间】:2020-07-20 16:13:46
【问题描述】:

现在我像这样显示我的对话框片段:

viewModel.onMoreMenuClickEvent.observe(this, Observer { event ->
    event.getContentIfNotHandled()?.let { menuItems ->
        dialogFragment = BottomMenuDialog.newInstance(menuItems = menuItems).also {
            it.setTargetFragment(this, MORE_MENU_REQUEST_CODE)
            activity?.showDialogFragment(it)
        }
    }
})

在我的nav_graph.xml 我添加了对话框:

<dialog
    android:id="@+id/bottomMenuDialog"
    android:name="com.mandarine.sai.features.menu.BottomMenuDialog"
    android:label="BottomMenuDialog" />

还有写导航的乐趣:

findNavController().navigate(R.id.bottom_menu_dialog)

但是我如何添加我在newInstanceit.setTargetFragment(this, MORE_MENU_REQUEST_CODE) 中发送的菜单项?

这里也是我newInstance()的代码:

fun newInstance(
    menuId: String = "",
    menuItems: List<MenuItemData>
): BottomMenuDialog {
    return BottomMenuDialog().apply {
        arguments = Bundle().apply {
            putString(KEY_ID, menuId)
            putSerializable(KEY_ITEMS, ArrayList(menuItems))
        }
    }
}

}

【问题讨论】:

    标签: kotlin android-jetpack-navigation


    【解决方案1】:

    查看安卓文档

    https://developer.android.com/guide/navigation/navigation-pass-data

    定义目标参数

    例子:

    在您的 xml 设置参数标签中

    当你得到de action时,发送de argument

    检查 id 动作命名

    还有

    在接收目的地的代码中,使用 getArguments() 方法检索包并使用其内容。在使用 -ktx 依赖项时,Kotlin 用户还可以使用 by navArgs() 属性委托来访问参数。

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多