创造Bundle
自己完成,然后将其添加到意图中应该会更快。
根据源代码 http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r1.2/android/content/Intent.java#Intent.putExtra%28java.lang.String,boolean%29, Intent.putExtra(String, String)
方法如下所示:
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
所以,它总是首先检查是否Bundle mExtras
已经创建了。这就是为什么在添加大量字符串时它可能会慢一些。Intent.putExtras(Bundle)
看起来像这样:
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}
所以,它会检查if (mExtras == null)
仅一次,稍后将所有值添加到内部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
由一个支持Map
(HashMap
准确地说),因此一次将所有字符串添加到此映射中也应该比逐个添加字符串更快。