New to Android here and am having a debate with a veteran over bundles and intents. This is what I've been doing...
Intent intent = new Intent(this, TargetActivity.class)
.putExtra(Constants.BUNDLE_ITEM_A, itemA)
.putExtra(Constants.BUNDLE_ITEM_B, itemB);
startActivity(intent);
He says that's wrong and you should be explicitly creating a new bundle, then passing it to 'putExtras', like so...
Intent intent = new Intent(this, TargetActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(Constants.BUNDLE_ITEM_A, itemA);
bundle.putSerializable(Constants.BUNDLE_ITEM_B, itemB);
intent.putExtras(bundle);
startActivity(intent);
However,'putExtras' already create a new bundle internally, then just merges in the passed-in bundle, essentially meaning it's a throw-away object (for this use-case). Here's the source for 'putExtras'...
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}
...so it seems like his approach is redundant, and actually wasteful as it creates an unnecessary bundle allocation, just to have it unparceled and merged with the one already in the intent.
So am I missing something? Is there a technical reason to do it the way he suggests?
Note: I understand using 'putExtras' to pass around bundles that were handed to you. This however is creating a new bundle simply to insert in a new intent so it seems unnecessary to me, but I could be wrong. That's why I'm asking about technical benefits to his approach.