前言
這周QA報了一個小bug,頁面A傳給頁面B的數(shù)據(jù)順序不對,查了一下代碼,原來頁面A中數(shù)據(jù)存儲容器用的是HashMap,而HasMap存取是無序的,所以傳給B去讀數(shù)據(jù)的時候,自然順序不對。
解決
既然HashMap是無序的,那我直接用LinkedHashMap來代替不就行了,大多數(shù)人估計看到這個bug時,開始都是這么想的。于是我就順手在HashMap前加了一個Linked,點了一下run,泯上一口茶,靜靜等待著奇跡的發(fā)生。
然而奇跡沒有來臨,奇怪的事反倒是發(fā)生了,B頁面收到數(shù)據(jù)后,居然報了一個類型強轉錯誤,B收到的是HashMap,而不是LinkedHashMap,怎么可能?。。?!我趕緊放下茶杯,review了一下代碼,沒錯啊,A頁面?zhèn)鬟f的確實是LinkedHashMap,但是B拿到就是HashMap,真是活見鬼了。
我立馬Google了一下,遇到這個錯誤的人還真不少,評論區(qū)給出的一種解決方案就是用Gson將LinkedHashMap序列化成String,再進行傳遞。。。由于bug催的緊,我也沒有去嘗試這種方法了,直接就放棄了傳遞Map,改用ArrayList了。不過后來看源碼,又發(fā)現(xiàn)了另外一種方式,稍后再說。
原因
Bug倒是解決了,但是Intent無法傳遞LinkedHashMap的問題還在我腦海里縈繞,我就稍微翻看了一下源碼,恍然大悟!
HashMap實現(xiàn)了Serializable接口,而LinkedHashMap是繼承自HashMap的,所以用Intent傳遞是沒有問題的,我們先來追一下A頁面?zhèn)鬟f的地方:
intent.putExtra("map",new LinkedHashMap<>());
接著往里看:
public Intent putExtra(String name, Serializable value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putSerializable(name, value);
return this;
}
intent是直接構造了一個Bundle,將數(shù)據(jù)傳遞到Bundle里,Bundle.putSerializable()里其實也是直接調(diào)用了父類BaseBundle.putSerializable():
void putSerializable(@Nullable String key, @Nullable Serializable value) {
unparcel();
mMap.put(key, value);
}
這里直接將value放入了一個ArrayMap中,并沒有做什么特殊處理。
事情到這似乎沒有了下文,那么這個LinkedHashMap又是何時轉為HashMap的呢?有沒有可能是在startActivity()中做的處理呢?
了解activity啟動流程的工程師應該清楚,startActivity()最后調(diào)的是:
ActivityManagerNative.getDefault().startActivity()
ActivityManagerNative是個Binder對象,其功能實現(xiàn)是在ActivityManagerService中,而其在app進程中的代理對象則為ActivityManagerProxy。所以上面的startActivity()最后調(diào)用的是ActivityManagerProxy.startActivity(),我們來看看這個方法的源碼:
public int startActivity(IApplicationThread caller, String callingPackage, Intent intent,
String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle options) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
......
intent.writeToParcel(data, 0);
......
int result = reply.readInt();
reply.recycle();
data.recycle();
return result;
}
注意到方法中調(diào)用了intent.writeToParcel(data, 0),難道這里做了什么特殊處理?
public void writeToParcel(Parcel out, int flags) {
out.writeString(mAction);
Uri.writeToParcel(out, mData);
out.writeString(mType);
out.writeInt(mFlags);
out.writeString(mPackage);
......
out.writeBundle(mExtras);
}
最后一行調(diào)用了Parcel.writeBundle()方法,傳參為mExtras,而之前的LinkedHashMap就放在這mExtras中。
public final void writeBundle(Bundle val) {
if (val == null) {
writeInt(-1);
return;
}
val.writeToParcel(this, 0);
}
這里最后調(diào)用了Bundle.writeToParcel(),最終會調(diào)用到其父類BaseBundle的writeToParcelInner():
void writeToParcelInner(Parcel parcel, int flags) {
// Keep implementation in sync with writeToParcel() in
// frameworks/native/libs/binder/PersistableBundle.cpp.
final Parcel parcelledData;
synchronized (this) {
parcelledData = mParcelledData;
}
if (parcelledData != null) {
......
} else {
// Special case for empty bundles.
if (mMap == null || mMap.size() <= 0) {
parcel.writeInt(0);
return;
}
......
parcel.writeArrayMapInternal(mMap);
......
}
}
可見最后else分支里,會調(diào)用Parcel.writeArrayMapInternal(mMap),這個mMap即為Bundle中存儲K-V的ArrayMap,看看這里有沒有對mMap做特殊處理:
void writeArrayMapInternal(ArrayMap<String, Object> val) {
if (val == null) {
writeInt(-1);
return;
}
// Keep the format of this Parcel in sync with writeToParcelInner() in
// frameworks/native/libs/binder/PersistableBundle.cpp.
final int N = val.size();
writeInt(N);
if (DEBUG_ARRAY_MAP) {
RuntimeException here = new RuntimeException("here");
here.fillInStackTrace();
Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
}
int startPos;
for (int i=0; i<N; i++) {
if (DEBUG_ARRAY_MAP) startPos = dataPosition();
writeString(val.keyAt(i));
writeValue(val.valueAt(i));
if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
+ (dataPosition()-startPos) + " bytes: key=0x"
+ Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
+ " " + val.keyAt(i));
}
}
在最后的for循環(huán)中,會遍歷mMap中所有的K-V對,先調(diào)用writeString()寫入Key,再調(diào)用writeValue()來寫入Value。真相就在writeValue()里:
public final void writeValue(Object v) {
if (v == null) {
writeInt(VAL_NULL);
} else if (v instanceof String) {
writeInt(VAL_STRING);
writeString((String) v);
} else if (v instanceof Integer) {
writeInt(VAL_INTEGER);
writeInt((Integer) v);
} else if (v instanceof Map) {
writeInt(VAL_MAP);
writeMap((Map) v);
}
......
......
}
這里會判斷value的具體類型,如果是Map類型,會先寫入一個VAL_MAP的類型常量,緊接著調(diào)用writeMap()寫入value。writeMap()最后走到了writeMapInternal():
void writeMapInternal(Map<String,Object> val) {
if (val == null) {
writeInt(-1);
return;
}
Set<Map.Entry<String,Object>> entries = val.entrySet();
writeInt(entries.size());
for (Map.Entry<String,Object> e : entries) {
writeValue(e.getKey());
writeValue(e.getValue());
}
}
可見,這里并沒有直接將LinkedHashMap序列化,而是遍歷其中所有K-V,依次寫入每個Key和Value,所以LinkedHashMap到這時就已經(jīng)失去意義了。
那么B頁面在讀取這個LinkedHashMap的時候,是什么情況呢?從Intent中讀取數(shù)據(jù)時,最終會走到getSerializable():
Serializable getSerializable(@Nullable String key) {
unparcel();
Object o = mMap.get(key);
if (o == null) {
return null;
}
try {
return (Serializable) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Serializable", e);
return null;
}
}
這里乍一看就是直接從mMap中通過key取到value,其實重要的邏輯全都在第一句unparcel()中:
synchronized void unparcel() {
synchronized (this) {
final Parcel parcelledData = mParcelledData;
if (parcelledData == null) {
if (DEBUG) Log.d(TAG, "unparcel "
+ Integer.toHexString(System.identityHashCode(this))
+ ": no parcelled data");
return;
}
if (LOG_DEFUSABLE && sShouldDefuse && (mFlags & FLAG_DEFUSABLE) == 0) {
Slog.wtf(TAG, "Attempting to unparcel a Bundle while in transit; this may "
+ "clobber all data inside!", new Throwable());
}
if (isEmptyParcel()) {
if (DEBUG) Log.d(TAG, "unparcel "
+ Integer.toHexString(System.identityHashCode(this)) + ": empty");
if (mMap == null) {
mMap = new ArrayMap<>(1);
} else {
mMap.erase();
}
mParcelledData = null;
return;
}
int N = parcelledData.readInt();
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ ": reading " + N + " maps");
if (N < 0) {
return;
}
ArrayMap<String, Object> map = mMap;
if (map == null) {
map = new ArrayMap<>(N);
} else {
map.erase();
map.ensureCapacity(N);
}
try {
parcelledData.readArrayMapInternal(map, N, mClassLoader);
} catch (BadParcelableException e) {
if (sShouldDefuse) {
Log.w(TAG, "Failed to parse Bundle, but defusing quietly", e);
map.erase();
} else {
throw e;
}
} finally {
mMap = map;
parcelledData.recycle();
mParcelledData = null;
}
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ " final map: " + mMap);
}
這里主要是讀取數(shù)據(jù),然后填充到mMap中,其中關鍵點在于parcelledData.readArrayMapInternal(map, N, mClassLoader):
void readArrayMapInternal(ArrayMap outVal, int N,
ClassLoader loader) {
if (DEBUG_ARRAY_MAP) {
RuntimeException here = new RuntimeException("here");
here.fillInStackTrace();
Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
}
int startPos;
while (N > 0) {
if (DEBUG_ARRAY_MAP) startPos = dataPosition();
String key = readString();
Object value = readValue(loader);
if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
+ (dataPosition()-startPos) + " bytes: key=0x"
+ Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
outVal.append(key, value);
N--;
}
outVal.validate();
}
這里其實對應于之前所說的writeArrayMapInternal(),先調(diào)用readString讀出Key值,再調(diào)用readValue()讀取value值,所以重點還是在于readValue():
public final Object readValue(ClassLoader loader) {
int type = readInt();
switch (type) {
case VAL_NULL:
return null;
case VAL_STRING:
return readString();
case VAL_INTEGER:
return readInt();
case VAL_MAP:
return readHashMap(loader);
......
}
}
這里對應之前的writeValue(),先讀取之間寫入的類型常量值,如果是VAL_MAP,就調(diào)用readHashMap():
public final HashMap readHashMap(ClassLoader loader){
int N = readInt();
if (N < 0) {
return null;
}
HashMap m = new HashMap(N);
readMapInternal(m, N, loader);
return m;
}
真相大白了,readHashMap()中直接new了一個HashMap,再依次讀取之前寫入的K-V值,填充到HashMap中,所以B頁面拿到就是這個HashMap,而拿不到LinkedHashMap了。
一題多解
雖然不能直接傳LinkedHashMap,不過可以通過另一種方式來傳遞,那就是傳遞一個實現(xiàn)了Serializable接口的類對象,將LinkedHashMap作為一個成員變量放入該對象中,再進行傳遞。如:
public class MapWrapper implements Serializable {
private HashMap mMap;
public void setMap(HashMap map){
mMap=map;
}
public HashMap getMap() {
return mMap;
}
}
那么為什么這樣傳遞就行了呢?其實也很簡單,因為在writeValue()時,如果寫入的是Serializable對象,那么就會調(diào)用writeSerializable():
public final void writeSerializable(Serializable s) {
if (s == null) {
writeString(null);
return;
}
String name = s.getClass().getName();
writeString(name);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(s);
oos.close();
writeByteArray(baos.toByteArray());
} catch (IOException ioe) {
throw new RuntimeException("Parcelable encountered " +
"IOException writing serializable object (name = " + name +
")", ioe);
}
}
可見這里直接將這個對象給序列化成字節(jié)數(shù)組了,并不會因為里面包含一個Map對象而再走入writeMap(),所以LinkedHashMap得以被保存了。
結論:
一句話,遇到問題就多看源碼!