Creating Bundle
on your own and then adding that to an intent should be faster.
According to the source code, Intent.putExtra(String, String)
method looks like this:
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
So, it will always check at first if Bundle mExtras
was already created. That's why it's probably a bit slower with big sequence of String additions. Intent.putExtras(Bundle)
looks like this:
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}
So, it will check if (mExtras == null)
only once and later add all the values to the internal Bundle mExtras
with Bundle.putAll()
:
public void putAll(Bundle map) {
unparcel();
map.unparcel();
mMap.putAll(map.mMap);
// fd state is now known if and only if both bundles already knew
mHasFds |= map.mHasFds;
mFdsKnown = mFdsKnown && map.mFdsKnown;
}
Bundle
is backed up by a Map
(HashMap
to be precise), so adding all Strings at once to this map should be also faster than adding Strings one by one.