Android“包裹”:Bundle
Bundle是谷歌封装好的“包裹”,官方的定义就是:A mapping from String keys to various Parcelable values。
1、Bundle介绍
Bundle位于android.os包中,用来携带数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。
用Bundle传递对象或对象数组时,必须实现Serializable 或Parcelable接口,关于Parcelable详见Android中的两种序列化:Parcelable和Serialable。
1.1、创建Bundle
使用Bundle直接new一个就可以了,构造一个空的Bundle对象。Bundle继承自BaseBundle,大部分功能,成员变量都封装在BaseBundle,继续往下看。
/**
* Constructs a new, empty Bundle.
*/
public Bundle() {
super();
mFlags = FLAG_HAS_FDS_KNOWN | FLAG_ALLOW_FDS;
}调用了BaseBundle构造函数,来声明一个Bundle对象。
/**
* Constructs a new, empty Bundle.
*/
BaseBundle() {
this((ClassLoader) null, 0);
}
/**
* Constructs a new, empty Bundle that uses a specific ClassLoader for
* instantiating Parcelable and Serializable objects.
*
* @param loader An explicit ClassLoader to use when instantiating objects
* inside of the Bundle.
* @param capacity Initial size of the ArrayMap.
*/
BaseBundle(@Nullable ClassLoader loader, int capacity) {
mMap = capacity > 0 ?
new ArrayMap<String, Object>(capacity) : new ArrayMap<String, Object>();
mClassLoader = loader == null ? getClass().getClassLoader() : loader;
}看到有一个熟悉的mMap,没错,这个Map就是Bundle帮我们保存数据的映射ArrayMap。
mMap = capacity > 0 ? new ArrayMap<String, Object>(capacity) : new ArrayMap<String, Object>();ArrayMap在android.util包中,继承了Map<K,V>接口,是谷歌提供给开发者的一种映射数据结构。虽然ArrayMap本身对键的类型没有限制,但是在Bundle中键值K已经被固定成String类型:
ArrayMap<String, Object> mMap;1.2、put数据方法
上面简短的看了下源码,不难发现向Bundle中添加数据的时候,键只能是String类型。Bundle提供了很多put数据的方法:
void | putAll(Bundle bundle)Inserts all mappings from the given Bundle into this Bundle. |
void | putBinder(String key, IBinder value)Inserts an |
void | putBundle(String key, Bundle value)Inserts a Bundle value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putByte(String key, byte value)Inserts a byte value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putByteArray(String key, byte[] value)Inserts a byte array value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putChar(String key, char value)Inserts a char value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putCharArray(String key, char[] value)Inserts a char array value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putCharSequence(String key, CharSequence value)Inserts a CharSequence value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putCharSequenceArray(String key, CharSequence[] value)Inserts a CharSequence array value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putCharSequenceArrayList(String key, ArrayList<CharSequence> value)Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putFloat(String key, float value)Inserts a float value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putFloatArray(String key, float[] value)Inserts a float array value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putIntegerArrayList(String key, ArrayList<Integer> value)Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putParcelable(String key, Parcelable value)Inserts a Parcelable value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putParcelableArray(String key, Parcelable[] value)Inserts an array of Parcelable values into the mapping of this Bundle, replacing any existing value for the given key. |
void | putParcelableArrayList(String key, ArrayList<? extends Parcelable> value)Inserts a List of Parcelable values into the mapping of this Bundle, replacing any existing value for the given key. |
void | putSerializable(String key, Serializable value)Inserts a Serializable value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putShort(String key, short value)Inserts a short value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putShortArray(String key, short[] value)Inserts a short array value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putSize(String key, Size value)Inserts a Size value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putSizeF(String key, SizeF value)Inserts a SizeF value into the mapping of this Bundle, replacing any existing value for the given key. |
void | putSparseParcelableArray(String key, SparseArray<? extends Parcelable> value)Inserts a SparceArray of Parcelable values into the mapping of this Bundle, replacing any existing value for the given key. |
void | putStringArrayList(String key, ArrayList<String> value)Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key. |
2、Android有哪些封装了Bundle?
框架中很多地方用到Bundle,比如:
①Message:信使Message:子线程数据携带到主线程
Message msg = Message.obtain();
Bundle bundle = new Bundle();
msg.setData(bundle);②Intent
Intent intent = new Intent();
Bundle bundle = new Bundle();
intent.putExtras(bundle);... ...

