Advantages of Using Bundle Instead of Direct Intent Putextra() in Android

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

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

Why would you put a new bundle in a new intent's extras vs setting extras directly on the new intent?

That's why I'm asking about technical benefits to his approach.

TL;TR: There's no benefit in the case you talk about. It's opposite.

Calling use of putExtra() wrong is rather plain silly, and quite exposes lack of knowledge of Intent internals. Your veteran should do quick look at Intent.java sources instead of blindly arguing as he'd then clearly see:

public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}

and what is putAll() doing? Docs say:

Inserts all mappings from the given Bundle into this Bundle.

So putExtras() simply inserts all mappings from the Bundle given as argument into Intent's internal bundle.

It's pretty clear at that point that creating separate Bundle by hand, then stuffing all extras into it just to pass that bundle to putExtras() brings completely zero benefits over direct suffing with bunch of putExtra() calls.

putExtras() is simply a helper method to let you mass-set extras (hence its name) from i.e. bundle you received as method argument, so if you already have a bundle in hand which you want to pass along, you'd putExtras(), but if you stuffing things yourself, using putExtra() makes more sense.

Intent and Bundle Relation

Sometimes you need to pass only a few variables or values to some Other Activity, but what if you have a bunch of variable's or values that you need to pass to various Activities. In that case you can use Bundle and pass the Bundle to the required Activity with ease. Instead of passing single variable's every time.

What is a bundle in an Android application

Bundles are generally used for passing data between various Android activities. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity.

You can use it like this:

Intent intent = new...
Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("myKey", AnyValue);
startActivity(intent);

You can get the passed values by doing:

Bundle extras = intent.getExtras(); 
String tmp = extras.getString("myKey");

You can find more info at:

  • android-using-bundle-for-sharing-variables and

  • Passing-Bundles-Around-Activities

Why is this putExtra passing a String instead of an int?

It's not telling you that you are getting a string, but that you are calling the getIntExtra method with insufficient parameters. Call it with

getIntExtra("image", 0); // or whichever default value you want to use.

Bundle.putParcelable(String key, Parcelable value) and Intent.putExtra(String name, Parcelable value) Difference

That's because Intent mechanism and the set/getArguments() of Fragment works differently.

When Activity contains Fragment it has direct reference to the Fragment, and setting a bundle on Fragment is simple set on the Fragment object, that's means that the Bundle object reference but the Activity is the same as the one in the Fragment. Actually you don't necessarily need this mechanism, as you're Activity probably controls the Fragments and knows their type, so you can just pass arguments by exposing a method on your Fragment. the benefit and the similarity to intent is that using provided set/getArguments() is retaining those arguments across fragment destroy and creation, those it uses Bundle and Parcelable, which are data that the system can stored outside your control.

But, Intent and Activity are different, Activity have no object reference to other Activities, and communicating must go through the system, and thus - unlike Activity/Fragment must be done with Parcelable/Bundle data that the system can serailze/deserialze.
When you sent an Intent to a different Activity your Parcelable goes through the system and get serailze/deserialze thus - you have a different Object reference in 2 differnt Activities. this is not happening when you setArgument on Fragment as Activity/Fragment has no boundaries like different Activities.

Passing string array between android activities

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);


Hope this will help you.

In order to read:

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);


Related Topics



Leave a reply



Submit