Android导航体系结构组件:如何将捆绑数据传递到startDestination [英] Android Navigation Architecture Component: How to pass bundle data to startDestination

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

问题描述

我有一个活动,该活动具有NavHostFragment.活动收到其意图的某些值.我想将此数据传递到导航图的第一个片段,即startDestination.我找不到与此有关的任何文档.

I have an activity which has a NavHostFragment. The activity receives certain values in its intent. I want to pass this data to the first fragment i.e startDestination of the navigation graph. I couldn't find any documentation regarding this.

我已经经历过关于SO的问题,但我似乎找不到navController.getGraph()addDefaultArguments方法.

I have already gone through this question on SO but I can't seem to find the addDefaultArguments method for navController.getGraph().

是否可以将捆绑软件传递给startDestination?

Is it possible to pass bundle to startDestination?

推荐答案

在更新的导航文档中找到正确的方法后,回答我自己的问题.

Answering my own question as I found the correct approach in the updated Navigation documentation.

在撰写此答案时,我正在使用Navigation 2.2.0-alpha01

At the time of writing this answer, I am using Navigation 2.2.0-alpha01

如果要直接将某些数据作为主机活动的参数传递到起始目标,则需要在主机活动的onCreate()方法中手动设置主机的导航图,如下所示:

If you want to pass some data to the start destination directly as arguments from host activity, you need to manually set your host’s navigation graph inside the host activity’s onCreate() method, as shown below:

让您获得navController:

Get you navController:

val navController by lazy { findNavController(R.id.<your_nav_host_id>) }

然后在主持人活动的onCreate()

val bundle = Bundle()
bundle.putString("some_argument", "some_value")
navController.setGraph(R.navigation.<you_nav_graph_xml>, bundle)

或者,如果您希望将整个Intent Extras传递给startDestination:

Or if you want to pass the whole intent extras as it is to the startDestination:

navController.setGraph(R.navigation.<you_nav_graph_xml>, intent.extras)

由于intent.extras仅返回Bundle

使用setGraph()方法设置navGraph时,应避免在以下位置设置app:NavGraph属性 NavHostFragment定义,因为这样做会导致膨胀 并设置两次导航图.

When you are setting the navGraph using setGraph() method, you should avoid setting the app:NavGraph attribute in the NavHostFragment definition, because doing so results in inflating and setting the navigation graph twice.

在startDestination片段中读取这些参数时:

While reading these arguments in your startDestination fragment:

如果您使用的是安全Args插件 (非常推荐),然后在您的片段中:

If you are using the Safe Args Plugin (which is very much recommended), then in your fragment:

private val args by navArgs<DummyFragmentArgs>()

Safe Args插件会通过将Args附加到您的片段名称来生成Args类.例如,如果您将片段称为DummyFragment,那么安全Args会生成一个名为DummyFragmentArgs

Safe Args plugin would generate an Args class by appending Args to your fragment name. For example, if you fragment is called DummyFragment then Safe Args would generate a class called DummyFragmentArgs

其中navArgs<> Android KTX

如果您未使用Android KTX,则可以获取args对象,例如:

If you are not using Android KTX, you can get the args object like:

val args = DummyFragmentArgs.fromBundle(arguments!!)

一旦获取了arguments对象,就可以简单地获取参数:

Once you've acquired the arguments object, you can simply fetch your arguments:

args.someArgument

注意我们如何将"some_argument"作为参数传递,并且我们正在使用安全Args将其作为someArgument读取

Notice how we passed "some_argument" as argument, and we are reading it as someArgument using Safe Args

如果您不使用安全Args(虽然没有理由不使用它),则可以这样访问参数:

If you are not using Safe Args (there is no reason to not use it though), you can access your arguments like this:

arguments?.getString("some_argument")

所有这些都记录在此处的迁移到导航组件"文档中: https://developer.android.com/guide/navigation/navigation-migrate#pass_activity_destination_args_to_a_start

All of this is documented in Migrate to Navigation Component documentation here: https://developer.android.com/guide/navigation/navigation-migrate#pass_activity_destination_args_to_a_start_destination_fragment

这篇关于Android导航体系结构组件:如何将捆绑数据传递到startDestination的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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