Android“包裹”:Bundle

QuibblerQuibbler 2019-12-31 约 15 分钟 1829 次阅读

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数据的方法:

voidputAll(Bundle bundle)

Inserts all mappings from the given Bundle into this Bundle.

voidputBinder(String key, IBinder value)

Inserts an IBinder value into the mapping of this Bundle, replacing any existing value for the given key.

voidputBundle(String key, Bundle value)

Inserts a Bundle value into the mapping of this Bundle, replacing any existing value for the given key.

voidputByte(String key, byte value)

Inserts a byte value into the mapping of this Bundle, replacing any existing value for the given key.

voidputByteArray(String key, byte[] value)

Inserts a byte array value into the mapping of this Bundle, replacing any existing value for the given key.

voidputChar(String key, char value)

Inserts a char value into the mapping of this Bundle, replacing any existing value for the given key.

voidputCharArray(String key, char[] value)

Inserts a char array value into the mapping of this Bundle, replacing any existing value for the given key.

voidputCharSequence(String key, CharSequence value)

Inserts a CharSequence value into the mapping of this Bundle, replacing any existing value for the given key.

voidputCharSequenceArray(String key, CharSequence[] value)

Inserts a CharSequence array value into the mapping of this Bundle, replacing any existing value for the given key.

voidputCharSequenceArrayList(String key, ArrayList<CharSequence> value)

Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key.

voidputFloat(String key, float value)

Inserts a float value into the mapping of this Bundle, replacing any existing value for the given key.

voidputFloatArray(String key, float[] value)

Inserts a float array value into the mapping of this Bundle, replacing any existing value for the given key.

voidputIntegerArrayList(String key, ArrayList<Integer> value)

Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key.

voidputParcelable(String key, Parcelable value)

Inserts a Parcelable value into the mapping of this Bundle, replacing any existing value for the given key.

voidputParcelableArray(String key, Parcelable[] value)

Inserts an array of Parcelable values into the mapping of this Bundle, replacing any existing value for the given key.

voidputParcelableArrayList(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.

voidputSerializable(String key, Serializable value)

Inserts a Serializable value into the mapping of this Bundle, replacing any existing value for the given key.

voidputShort(String key, short value)

Inserts a short value into the mapping of this Bundle, replacing any existing value for the given key.

voidputShortArray(String key, short[] value)

Inserts a short array value into the mapping of this Bundle, replacing any existing value for the given key.

voidputSize(String key, Size value)

Inserts a Size value into the mapping of this Bundle, replacing any existing value for the given key.

voidputSizeF(String key, SizeF value)

Inserts a SizeF value into the mapping of this Bundle, replacing any existing value for the given key.

voidputSparseParcelableArray(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.

voidputStringArrayList(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);

... ...

相关推荐

精选
ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager
Android

ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager

ViewPager1、ViewPagerandroidx.viewpager.widget.ViewPager,Android中使用非常广泛的控件,可以说是APP必备:首次打开引导页、页面Banner广告等。常用方法:setAdapter() 设置适配器setOffscreenPageLimit() 设置缓存的页面个数,默认是 1setCurrentItem() 跳转到特定的页面setOnPage

1.1k
获取Android内置WebView内核版本
Android

获取Android内置WebView内核版本

获取Android内置WebView内核版本竟然能遇到这样奇葩的事情,网页用的技术过于新颖,以至于只支持高版本Chromium内核的,低版本安卓系统中内置的内核版本较低,无法加载前端页面。1、设置查看在系统设置里 > 应用 > 应用管理 > 显示系统应用,查看WebView组件:2、页面查看通过WebView发起的网络请求,都会带上浏览器的UA,通常页面都可以通过UA判断浏览器的内核版本。这里有两

1.1w
Android 14适配总结
Android

Android 14适配总结

Android 14适配总结毕业工作至今已经适配了三个Android大版本,从Android 11到Android 12、再到Android 13。2023年,Google即将推出的Android 14,上半年已经开始第一批适配。现在,第四个Android版本已经适配完,总结记录一下。1、Android 14计划Google一般会在2月份对外发布预告,同时放出开发者预览版。“拉通”各大平台、厂商以

1.1w