如何使用 Android 导航将数据传递到前一个片段?

发布于 2025-01-15 20:46:51 字数 308 浏览 0 评论 0原文

我的应用程序以 NewAdFragment 开头,在这个片段中,有一个按钮允许用户选择类别,当用户单击该按钮时,我将把他导航到 ChooserFragment通过传递类别列表作为参数,此片段内有 RecyclerView,它显示类别。

我想要什么?我希望当用户从RecyclerView中选择一个项目时,它必须返回到具有特定数据的NewAdFragment

我可以用很多方法解决这个问题,但也许导航组件库中已经存在一种方法。

最好的方法是什么?

My app starts with NewAdFragment and inside this fragment, There is a button that allows user to choose the category, When the user clicks on the button I'll navigate him to ChooserFragment with passing categories list as an argument, There is RecyclerView inside this fragment and it shows categories.

What do I want? I want when the user chooses an item from RecyclerView, Then it must back to NewAdFragment with specific data.

I can solve this problem in a lot of ways but maybe there is a method that exists in the navigation component library already.

What is the best way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-01-22 20:46:51

尝试实现此处提到的以下代码块:

FRAGMENT A

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val navController = findNavController();
// We use a String here, but any type that can be put in a Bundle is supported
navController.currentBackStackEntry?.savedStateHandle?.getLiveData<String>("key")?.observe(
    viewLifecycleOwner) { result ->
    // Do something with the result.
  }
}

FRAGMENT B

navController.previousBackStackEntry?.savedStateHandle?.set("key", result)
navController.popBackStack()

您还可以检查这个

try to implement below code blocks as mentioned here:

FRAGMENT A

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val navController = findNavController();
// We use a String here, but any type that can be put in a Bundle is supported
navController.currentBackStackEntry?.savedStateHandle?.getLiveData<String>("key")?.observe(
    viewLifecycleOwner) { result ->
    // Do something with the result.
  }
}

FRAGMENT B

navController.previousBackStackEntry?.savedStateHandle?.set("key", result)
navController.popBackStack()

you can also check this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文