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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11900266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:47:33  来源:igfitidea点击:

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

javaandroideclipseandroid-intent

提问by Alfred James

This question may sound stupid but I wana know When do we put activity name in Intent.putExtra()? In one case we are putting extra only with bundle and in other case we are passing it with class name. I am a little confused should we use Intent.putExtra(String, Bundle)we already have passed the activity name in Intentconstructor or not?

这个问题可能听起来很愚蠢,但我想知道我们什么时候输入活动名称Intent.putExtra()?在一种情况下,我们只用 bundle 放置额外的东西,而在另一种情况下,我们用类名传递它。我有点困惑我们是否应该使用Intent.putExtra(String, Bundle)我们已经在Intent构造函数中传递了活动名称?

Thanks for your help!

谢谢你的帮助!

回答by helios

I think you mean putExtra(String, Bundle)vs putExtras(Bundle)(with s).

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

The first adds the bundle as the value for the key you provide. The bundle is simple an object value.

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

The second adds all the key/value pairs from the provided bundle to the intent. In this case the content of the bundle will be added to the intent, not the bundle itself.

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

Think of them as in Mapinterface:

把它们想象成Map接口:

Map.put(String key, Object value)

vs

对比

Map.putAll(Map anotherMap)

回答by Tobias Moe Thorstensen

The approach is just the difference here. If you use a Bundleyou can store almost all types in it:

方法只是这里的区别。如果您使用 a Bundle,则可以在其中存储几乎所有类型:

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

and pass it to an activity

并将其传递给活动

mIntent.putExtras(mBundle);

and in the other activity which recieves the info, just grab the content of the bundle like this:

在另一个接收信息的活动中,只需像这样获取包的内容:

   Bundle extras = getIntent().getExtras();

and grab each element in the bundlelike this:

bundle像这样抓取每个元素:

extras.getString("myKey")