Bundle的使用与解析
Bundle
应用场景举例
Activity:onCreat()、onSaveInstanceState()
Fragment: setArguments()
Message:setData()
源码
//位于android.os包中,final类
public final class Bundle extends BaseBundle implements Cloneable, Parcelable
//实现了Cloneable, Parcelable接口,因此需要重写的方法
public Object clone()
public int describeContents()
public void writeToParcel(Parcel parcel, int flags)
public void readFromParcel(Parcel parcel)
public static final Parcelable.Creator<Bundle> CREATOR = new Parcelable.Creator<Bundle>()
构造方法
Bundle()
Bundle(ClassLoader loader)
Bundle(int capacity)
Bundle(Bundle b)
Bundle(PersistableBundle b)
获取只包含一个键值对的Bundle对象
public static Bundle forPair(String key, String value) {Bundle b = new Bundle(1);b.putString(key, value);return b;
}
put与get方法
put()与get()中的key值均为String类型,put()的value值类型为传入的数据类型
相关保存方法 | 相关读取方法 |
---|---|
putBoolean() | getBoolean() |
putByte() | getByte() |
putChar() | getChar() |
putShort() | getShort() |
putFloat() | getFloat() |
putCharSequence() | getCharSequence() |
putParcelable() | getParcelable() |
putSize() | getSize() |
putSizeF() | getSizeF() |
putParcelableArray() | getParcelableArray() |
putParcelableArrayList() | getParcelableArrayList() |
putSparseParcelableArray() | getSparseParcelableArray() |
putIntegerArrayList() | getIntegerArrayList() |
putStringArrayList() | getStringArrayList() |
putCharSequenceArrayList() | getCharSequenceArrayList() |
putSerializable() | getSerializable() |
putBooleanArray() | getBooleanArray() |
putByteArray() | getByteArray() |
putShortArray() | getShortArray() |
putCharArray() | getCharArray() |
putFloatArray() | getFloatArray() |
putCharSequenceArray() | getCharSequenceArray() |
putBundle() | getBundle() |
putBinder() | getBinder() |
清除Bundle数据
清除Bundle中所有数据:clear()
Bundle存取数据具体实现
内部维护了一个ArrayMap,具体定义是在其父类BaseBundle中
void putBoolean(String key, boolean value) {unparcel();//mMap即为ArrayMapmMap.put(key, value);
}
boolean getBoolean(String key) {unparcel();if (DEBUG) Log.d(TAG, "Getting boolean in "+ Integer.toHexString(System.identityHashCode(this)));return getBoolean(key, false);
}
//getBoolean()
boolean getBoolean(String key, boolean defaultValue) {unparcel();//通过key从ArrayMap里读出保存的数据Object o = mMap.get(key);if (o == null) {return defaultValue;}try {//转换成对应的类型返回return (Boolean) o;} catch (ClassCastException e) {typeWarning(key, o, "Boolean", defaultValue, e);//转换异常时返回缺省值return defaultValue;}
}
ParcelledData解析
三种取值情况:
mParcelledData = EMPTY_PARCEL
mParcelledData = Parcel.obtain()
mParcelledData = null
//定义,mParcelledData = null
Parcel mParcelledData = null;
//创建Bundle
BaseBundle(BaseBundle b) {if (b.mParcelledData != null) {if (b.mParcelledData == EMPTY_PARCEL) {//EMPTY_PARCELmParcelledData = EMPTY_PARCEL;} else {//Parcel.obtain()mParcelledData = Parcel.obtain();mParcelledData.appendFrom(b.mParcelledData, 0, b.mParcelledData.dataSize());mParcelledData.setDataPosition(0);}} else {mParcelledData = null;}if (b.mMap != null) {mMap = new ArrayMap<String, Object>(b.mMap);} else {mMap = null;}mClassLoader = b.mClassLoader;
}
unparcel()方法解析
/* package */ synchronized void unparcel() {if (mParcelledData == null) {//如果mParcelledData为null,直接返回if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))+ ": no parcelled data");return;}//如果mParcelledData为EMPTY_PARCEL,创建一个容量为1的ArrayMap对象if (mParcelledData == EMPTY_PARCEL) {if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))+ ": empty");if (mMap == null) {mMap = new ArrayMap<String, Object>(1);} else {mMap.erase();}//mParcelledData置为nullmParcelledData = null;return;}//如果mParcelledData为Parcel.obtain(),创建一个ArrayMap,再将数据存储到ArrayMap中int N = mParcelledData.readInt();if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))+ ": reading " + N + " maps");if (N < 0) {return;}if (mMap == null) {mMap = new ArrayMap<String, Object>(N);} else {mMap.erase();mMap.ensureCapacity(N);}mParcelledData.readArrayMapInternal(mMap, N, mClassLoader);//mParcelledData回收并置为nullmParcelledData.recycle();mParcelledData = null;if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this)) + " final map: " + mMap);
}
Bundle的使用与解析相关推荐
- 《ArcGIS Runtime SDK for Android开发笔记》——(12)、自定义方式加载Bundle格式缓存数据...
随着ArcGIS 10.3的正式发布,Esri推出了新的紧凑型缓存格式以增强用户的访问体验.新的缓存格式下,Esri将缓存的索引信息.bundlx包含在了缓存的切片文件.bundle中.具体如下图所示 ...
- 《深入理解OSGi:Equinox原理、应用与最佳实践》一3.2 Bundle状态及转换
3.2 Bundle状态及转换 "状态"是Bundle在运行期的一项动态属性,不同状态的Bundle具有不同的行为.生命周期层规范定义了Bundle生命周期过程之中的6种状态,分别 ...
- iOS 歌词解析(lrc, 非谓词, 仿QQ音乐, 仿卡拉ok模式)
前言 要解析 lrc 格式的歌词, 首先需要知道什么是 lrc 歌词, 还需要知道 lrc 歌词的规范. 在这里先放出一个百度百科的链接地址, 仅供大家参考: 百度百科: lrc 关于本文 本文的歌词 ...
- 从HarmonyOS应用到底是不是Android套壳?
注:文章来自于各位大佬分析总结,单纯技术论证,不吹不黑任何企业公司个人. 随着鸿蒙系统2.0的发布,各界一片争议,支持与反对.看好和看衰.「自主的全场景分布式系统」和「Android套壳」各执一词,吵 ...
- Fragment初识
概述 官方API Fragment是什么 Android 在 Android 3.0(API 11 级)中引入了Fragment,主要是为了给大屏幕(如平板电脑)上更加动态和灵活的 UI 设计提供支持 ...
- 探索 OSGi 框架的组件运行机制
在目前的 Java 开发平台中,对于组件开发过程,比如打包.部署和验证等,并没有一个统一的标准.正因如此,许多 Java 项目,例如 JBoss 和 Net Beans,都拥有一套自定义的组件开发规范 ...
- Android项目笔记整理(1)
第二部分 工作项目中以及平时看视频.看书或者看博客时整理的个人觉得挺有用的笔记 1.Activity界面切换: if(条件1){ setContentView(R.layout. ...
- 未能加载文件或程序集或它的某一个依赖项_手写一个miniwebpack
前言 之前好友希望能介绍一下 webapck 相关的内容,所以最近花费了两个多月的准备,终于完成了 webapck 系列,它包括一下几部分: webapck 系列一:手写一个 JavaScript 打 ...
- Activity之间传递参数
发送 intent.setClass(Bmi.this, Report.class); Bundle bundle = new Bundle(); bundle.putString("KEY ...
最新文章
- ​中国开启开源新纪元
- 百度首席科学家吴恩达谈百度人工智能项目进展情况
- 计算机网络基础 — 网络设备 — 以太交换机(Switch)
- Cissp-【第1章 安全和风险管理】-2021-1-7(99页-162页)
- 以下不是python文件读写方法的是-python 文件读写 - 刘江的python教程
- Android中三种常用解析XML的方式(DOM、SAX、PULL)简介及区别
- Vue 学习第四天--第一部分 --盲点整理与昨天知识回顾
- Win11任务栏大小怎么更改
- 背景纹理素材|为前景元素添加焦点
- 电力企业信息化建设解决方案之计量生产分析系统
- BIOS和DOS中断例程的安装过程
- IT公司、软件公司资质
- vcpkg:If you are sure you want to rebuild the above packages, run the command with the --recurse opt
- 信息系统项目管理师——范围管理论文
- 网付代理利润我把它分析得透透的
- 【镜像取证篇】VMware虚拟机配置文件取证
- xposed框架报错安装不上解决办法
- 纯swift开发,弹幕,演唱会广告牌
- Vijos - 古韵之鹊桥相会(最短路||DFS)
- 企业资产盘点系统能做什么