导航体系结构组件-将参数数据传递给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读取args.如果您不使用安全的参数,则可以从自己使用的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天全站免登陆