Android中使用Bundle代替直接Intent putExtra()的优点 [英] Advantages of using Bundle instead of direct Intent putExtra() in Android

查看:23
本文介绍了Android中使用Bundle代替直接Intent putExtra()的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 android 应用程序中,我总是使用 Intent 类的直接 putExtra() 函数将任意数量的值传递给新的 Activity.
像这样:

In my android application I'm always using direct putExtra() function of Intent class to pass any number of value to new Activity.
Like this:

Intent i = new Intent(this, MyActivity.class);
i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");
startActivity(i);

我知道 Android 中的 Bundle 并且我看到人们使用 Bundle 将值传递给新的 Activity.
像这样:

I know about Bundle in Android and I have seen people are using Bundle for passing values to new Activity.
Like this:

Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_USERNAME","my_username");
extras.putString("EXTRA_PASSWORD","my_password");
intent.putExtras(extras);
startActivity(intent);

这里我有 2 个疑问.
如果我可以通过将值直接放入 Intent<将值传递给新的 Activity,我为什么要使用 Bundle/代码>?
使用 Bundle 代替直接的 Intent putExtra() 有什么好处?

Here I have 2 doubts.
Why should I use Bundle if I can pass values to new Activity by putting it directly to Intent?
What are the advantages of using Bundle instead of direct Intent putExtra()?

推荐答案

它的作用很小(如果有什么区别的话).使用附加包的代码稍微重一些(在任何实际应用中都不会产生任何区别)并且更易于管理,更通用.

It makes little (if any difference). The code using an additional bundle is slightly heavier (it won't make any difference in any practical application) and slightly easier to manage, being more general.

如果有一天你决定 - 在将信息发送到一个意图中之前 - 你想要将数据序列化到数据库 - 拥有一个可以序列化的包,添加到一个意图,然后提供给一个PendingBundle - 一个对象.

If one day you decide that - before sending information inside an intent - you want to serialize the data to database - it will be a bit cleaner to have a bundle that you can serialize, add to an intent and then feed to a PendingBundle - all with one object.

[更新]

澄清(因为其他一些答案).

A clarification (because of some other answers).

Extras 每个 Intent 可能携带(但不是必须)的附加包,因此在使用包或不使用包之间别无选择.无论哪种方式,您都在使用捆绑包.

Extras is an additional bundle that each Intent might carry (but doesn't have to), so there is no alternative between using a bundle or not using it. You are using a bundle either way.

第一次使用 putExtra 时,Intent 中的 mExtras 包会被初始化,然后所有的 putExtra 都被委托给它.您无法访问捆绑包本身(这是设计使然,以避免某些类型的错误).

The first time you use putExtra, a mExtras bundle inside Intent is initialized and all the following putExtra are delegated to it. The bundle itself is inaccessible to you (this is by design, to avoid certain kind of bugs).

putExtras 不会将您的包放入 Intent 中.相反,它将它复制到当前的意图包中(或创建一个,就像 putExtra 一样).这就是为什么它稍微重一些(你有两包而不是一包并支付复制的费用).

putExtras does not put your bundle inside Intent. Instead, it copies it over to the current intent bundle (or creates one, as with putExtra). This is why it's slightly heavier (you have two bundles instead of one and pay the price of copying).

关键是 - 如果您使用 putExtras,您仍然无法访问意图中的真正包.但是 - 你有一个副本,你可能想用它做任何其他事情.喜欢保留复制到另一个意图(如果您发送很多类似的意图).

The crux is - if you use putExtras, you still cannot access the real bundle inside the intent. BUT - you have a copy for whatever else you might want to do with it. Like keep around to copy into another intent (if you send a lot of similar intents).

这篇关于Android中使用Bundle代替直接Intent putExtra()的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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