Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 652
Vishal Vijay Avatar asked Mar 06 '13 09:03

Vishal Vijay


People also ask

What is the difference between bundle and intent in android?

Bundles are used with intent and values are sent and retrieved in the same fashion, as it is done in the case of Intent. It depends on the user what type of values the user wants to pass, but bundles can hold all types of values (int, String, boolean, char) and pass them to the new activity.

What is the importance of the putExtra () method in android how it is different from setData ()?

putExtra allows you to add primitive (or parcelable) key-value pairs. setData is limited to passing an Uri . setData is conventionally used for the case of requesting data from another source, such as in startActivityForResult. but an uri can be sent through putextra also.

What is the putExtra () method used with intent for?

putExtra() method is used for send the data, data in key-value pair key is variable name and value can be Int, String, Float etc.

What is the use of putExtra in Android?

putExtra() adds extended data to the intent. It has two parameters, first one specifies the name which of the extra data,and the second parameter is the data itself.


2 Answers

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

like image 114
fdreger Avatar answered Sep 17 '22 21:09

fdreger


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

like image 20
vanguard69 Avatar answered Sep 20 '22 21:09

vanguard69