导航架构组件 - 将参数数据传递给 startDestination [英] Navigation Architecture Component- Passing argument data to the startDestination

本文介绍了导航架构组件 - 将参数数据传递给 startDestination的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动 A,它启动活动 B 向它传递一些意图数据.活动 B 托管来自新导航架构组件的导航图.我想将该意图数据作为参数传递给 startDestination 片段,该怎么做?

I have an activity A that start activity B passing to it some intent data. Activity B host a navigation graph from the new Navigation Architecture Component.I want to pass that intent data to the startDestination fragment as argument how to do that?

推荐答案

好的,感谢 Google 团队的 Ian Lake,我找到了该问题的解决方案.假设您有一个活动 A,它将使用一些意图数据启动活动 B,并且您想在 startDestination 中获取该数据,如果您使用安全参数,那么这里有两个选择,这是我的情况,您可以这样做

OK, I found a solution to that problem thanks to Ian Lake from the Google team. Let say you have an activity A that will start activity B with some intent data and you want to get that data in the startDestination you have two options here if you using safe args which is my case you could do

StartFragmentArgs.fromBundle(requireActivity().intent?.extras)

从 Intent 中读取参数.如果你不使用安全参数,你可以从你自己使用的包中提取数据 requireActivity().intent?.extras 这将返回一个你可以使用的包而不是片段 getArguments() 方法.就是这样,我试了一下,一切正常.

to read the args from the Intent. If you don't use safe args you can extract the data from the bundle your self-using requireActivity().intent?.extras which will return a Bundle you can use instead of the fragment getArguments() method. That's it I try it and everything works fine.

这篇关于导航架构组件 - 将参数数据传递给 startDestination的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆