Advantages of using Bundle instead of direct Intent putExtra() in Android

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

How to use ‘bundle’ instead of ‘intent.putExtra()’ in Fragment in Android

In Activity I have used the following code for passing the value to other Activity classes: intent.putExtra(book_arr, book_arr); // [putExtra(String *name*,Serializable *Value*)] How to use like th

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

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.

What is faster? One intent.putExtras(Bundle with Strings) or many intent.putExtra(String)?

What is faster? Adding a bunch of string values to a bundle and then adding that to an intent? Or just adding the values to an intent using intent.putExtra()? Or doesn’t it make much difference? A Go

Android – Intent “putExtra”

I have the next code: ´ ArrayList<Integer> vc = new ArrayList<Integer>; vc.add(1); vc.add(2); Intent myIntent = new Intent(class1.this, class2.class); myIntent.putIntegerArrayListExtra(Ke

Android Notification Intent.putExtra

I have a notification that when pressed should launch an activity that in turn launches a dialog box. That all works, but the dialog box pulls info from the notification thought Intent.putExtra(). The

Bundle.putExtra problem with android

I am currently developing an android app where I create a notification Intent which adds parameters to the bundle of the intent. When the notification is clicked it calls an activity and gets the data

Android – Intent.putExtra() Failing

I can’t seem to get my extra information to get sent along with an intent. The intent loads the next activity properly, but without the payload. Caller: //a list item was clicked protected void onLis

putExtra to intent in android geofencing API

pretty straightforward question: Is Intent encapsulated in PendingIntend (which is added as geofence in addGeofences(…) method) supposed to bear infomration in its extra? code: private PendingIntent

Android Intent putExtra(String, Serializable)

I’m sorry if this Question is already answered, i searched a lot, but i couldn’t find any question with my problem. I’m writing an android app which gets data from an internet database. My first activ

MMS in android not working using Intent.ACTION_SEND

How can i send an MMS in android ? My code using UI as follows : Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(address, 5556); intent.putExtra(sms_body, Gudmng !!); intent.ad

Answers

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

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

you can refer this and also this.

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.

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