80

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);

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);

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()?

Vishal Vijay
  • 2,334
  • 2
  • 20
  • 41

4 Answers4

67

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.

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.

[update]

A clarification (because of some other answers).

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.

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 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).

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).

fdreger
  • 11,377
  • 1
  • 33
  • 39
  • Tell me if I use a code like this Intent intent = new Intent(this, MyActivity.class); intent.putExtra(ID_EXTRA1, "1"); Bundle extras = new Bundle(); extras.putString("EXTRA_USERNAME","my_username"); extras.putString("EXTRA_PASSWORD","my_password"); intent.putExtras(extras); startActivity(intent); How can I access ID_EXTRA1 – Vishal Vijay Mar 06 '13 at 10:14
  • @VishalVijay you can get a copy of the extras bundle (not the original one) by calling getExtras. Careful, it could return null. – fdreger Mar 06 '13 at 10:21
  • So I can get ID_EXTRA1 and extras(bundle) from the new activity rigt. – Vishal Vijay Mar 06 '13 at 10:25
  • Sorry, I couldn't quite tell from the answer to this question but, if I have an intent which is for the sole purpose of editing a ValueObjects data, all I do is pass in the index of which VO to use from a static data resource. Since its mandatory should this be done in `startActivity(intent, options)` or `intent.putExtra()`? I know it can be done both, but I'm just thinking best practice? I do not usually touch the Bundle passed to onCreate but I am inexperienced so seeking opinions. – WORMSS Jul 03 '13 at 14:22
  • putExtras(bundle) does NOT delegate to the original intent. The original intent has an extra that is a bundle. – Lena Bru Oct 31 '15 at 13:19
  • @LenaBru: I do not understand the comment? What "original intent"? Did you mean "original bundle"? Is there something in the answer that you don't agree with? – fdreger Nov 02 '15 at 06:16
  • your answer states : "putExtras does not put your bundle inside Intent. Instead, it copies it over to the current intent " that is completely incorrect. You get an extra that is a bundle, it does not copy the contents of the bundle onto the intent extras – Lena Bru Nov 02 '15 at 08:28
  • @LenaBru Perhaps you confused putExtras with putExtra(String, Bundle)? My answer is easy to confirm. Just look into the putExtras method, it does exactly what I claimed it does. – fdreger Nov 02 '15 at 09:35
  • I have confirmed it, and it does not delegate to the intent. if i want that bundle i get it with intent.getExtras(), not with intent.getSomethingExtra(...) – Lena Bru Nov 02 '15 at 12:25
  • @LenaBru I don't understand your insistence in accusing me of being wrong. My answer is 100% correct, it can be proven by looking at the code of Intent class (which you can google in under one minute). When you pass a bundle to putExtras, the data from the bundle is copied to Intent's internal bundle. – fdreger Nov 04 '15 at 07:58
17

Additional advantage: Once data is put in a Bundle, you can send the same data through multiple intents. (Only in the case, multiple intents are to be sent).

vanguard69
  • 810
  • 1
  • 10
  • 18
6

Bundles are cool because you can isolate their creation/reading more easily, therefore separating the code handling the bundles from the code of the UI.

In most case that's useless as you'll want to transmit the smallest possible amount of data (usually just a couple of strings, an id ...)

njzk2
  • 37,030
  • 6
  • 63
  • 102
5

you can refer Intent and Bundle Relation (Stackoverflow) and also What is the importance of bundle in an Android program (Stackoverflow).

you can send multiple or bunch of data in one bundle and send it through Intent. or another way is add multiple statements of PutExtra().

And there is not any such important difference as per my knowledge.

carloswm85
  • 326
  • 3
  • 10
Mahaveer Muttha
  • 1,657
  • 4
  • 19
  • 32