繁体   English   中英

Intent.putExtra(String,Bundle)vs Intent.putExtra(Bundle)

[英]Intent.putExtra(String,Bundle) vs Intent.putExtra(Bundle)

这个问题可能听起来很愚蠢,但我知道什么时候把活动名称放在Intent.putExtra() 在一种情况下,我们只使用bundle进行额外的操作,而在其他情况下,我们使用类名传递它。 我有点困惑,如果我们使用Intent.putExtra(String, Bundle)我们已经在Intent构造函数中传递了活动名称了吗?

谢谢你的帮助!

我认为你的意思是putExtra(String, Bundle) vs putExtras(Bundle) (带s )。

第一个添加捆绑包作为您提供的密钥的值。 捆绑包是简单的对象值。

第二个将所提供的包中的所有键/值对添加到intent。 在这种情况下,捆绑包的内容将添加到intent中,而不是捆绑包本身。

将它们想象为Map接口:

Map.put(String key, Object value)

VS

Map.putAll(Map anotherMap)

这种方法与众不同。 如果您使用Bundle您可以在其中存储几乎所有类型:

Bundle mBundle = new Bundle();
mBundle.put(key, value);

并将其传递给一个活动

mIntent.putExtras(mBundle);

在收到信息的其他活动中,只需抓住包的内容,如下所示:

   Bundle extras = getIntent().getExtras();

并抓住bundle每个元素,如下所示:

extras.getString("myKey")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM