intent传递一个对象(不是int,long那些基类型),是传递的引用地址,还是new出了一个新的对象传过去的?
根据源码分析,传递的是一个引用,就拿paceable类型的对象来说:
(1)存储数据的时候Bundle中的源码:
第一步:
public Intent putExtra(String name, Parcelable value) {
if (mExtras == null) {
mExtras = new Bundle();
}
》》重点看这句mExtras.putParcelable(name, value);
return this;
}
第二步
/**
* Inserts a Parcelable value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* @param key a String, or null
* @param value a Parcelable object, or null
*/
public void putParcelable(@Nullable String key, @Nullable Parcelable value) {
unparcel();
》》重点看这句 mMap.put(key, value);
mFlags &= ~FLAG_HAS_FDS_KNOWN;
}
放入一个mMap中,而mMap的数据类型是 ArrayMap<K, V>
ArrayMap中的
@Override
public V put(K key, V value)方法中仅仅是赋值, mArray[index] = value;
所以在放的时候是一个引用
(2) 在取的时候也没有创建
*/
@Nullable
public <T extends Parcelable> T getParcelable(@Nullable String key) {
unparcel();
》》重点看这句 Object o = mMap.get(key); //是Bundle中的源码,直接从map中取出然后返回
if (o == null) {
return null;
}
try {
return (T) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Parcelable", e);
return null;
}
}
综上所述,传递的是引用