Android简介:Bundle
一、Bundle
Bundle主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。
经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。
二、使用( Android Activity 数据传递:一次传多个)
Intent itActivity = new Intent(MainActivity.this,SecondActivity.class);Bundle bd = new Bundle();bd.putString("name","小明");bd.putInt("age",22);itActivity.putExtras(bd);startActivity(itActivity);
第二个Activity
Intent itActivity2 = getIntent();
Bundle bd = itActivity2.getExtras();
String name = bd.getString("name");
int age = bd.getInt("age");
Log.d("intent","name:"+name+" age:"+age);
------------------------------------------------------------------------------------------------------------------------
Parcelable类型的对象
Parcelable是Android自定义的一个接口,它包括将数据写入Parcel和从Parcel中读出的API。一个实体(用类来表示),如果需要封装到Bundle中去,可以通过实现Parcelable接口来完成。
public interface Parcelable { //内容描述接口,基本不用管 public int describeContents(); //写入接口函数,打包 public void writeToParcel(Parcel dest, int flags); //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。 //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。 public interface Creator<T> { public T createFromParcel(Parcel source); public T[] newArray(int size); }
}
1.实现Parcelable接口
看看一个官方给出的例子:
注意:对象中定义的CREATOR不可以改为其他名字噢
public class MyParcelable implements Parcelable {private int mData;public int describeContents() {return 0;}public void writeToParcel(Parcel out, int flags) {out.writeInt(mData);}public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {public MyParcelable createFromParcel(Parcel in) {return new MyParcelable(in);}public MyParcelable[] newArray(int size) {return new MyParcelable[size];}};private MyParcelable(Parcel in) {mData = in.readInt();}
}
这个例子中的对象只包含了一个属性mData,我们再看一个例子
public class User implements Parcelable {private String name;private int age;public User(String name, int age) {this.name = name;this.age = age;}protected User(Parcel in) {name = in.readString();age = in.readInt();}public static final Creator<User> CREATOR = new Creator<User>() {@Overridepublic User createFromParcel(Parcel in) {return new User(in);}@Overridepublic User[] newArray(int size) {return new User[size];}};@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(age);}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}
}
2.传递Parcelable对象并解析
传递:
User user = new User("shellhub", 23);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(KEY, user);
startActivity(intent);
解析:
User user = getIntent().getExtras().getParcelable(KEY);
因为方法实现了泛型,所以不需要类型转换
public T getParcelable(@Nullable String key) {
Android简介:Bundle相关推荐
- Android App Bundle 和Unity AAB BundleTools
1.为什么要用Android App Bundle 从 2021年8月起,新应用需要使用 Android App Bundle 才能在 Google Play 中发布.现在,Play Feature ...
- Android App Bundle混淆加密加壳加固保护的解决方案(过Google App上架审核)
Android AAB简介和AAB包格式 AAB即Android App Bundle,是Google官方发布的一种新的App包格式,可以有效缩减App大小,提升用户安装和更新App的体验.在Goog ...
- Android App Bundle
1. Android App Bundle 是什么? 从 2021 年 8 月起,新应用需要使用 Android App Bundle 才能在 Google Play 中发布. Android App ...
- 分享篇 - 58同城基于Android APP Bundle开发的全新编译模式(编译速度提升70%)
目录 1. Wafers 项目背景 2. 效果展示 3. 实现方案 4. 改造期间遇到的问题 5. 如何接入使用 6. 对比 Instant Run 和 Apply Changes 7. 总结 1. ...
- Android App Bundle打包发布GooglePlay
Android App Bundle 简介官方文档 Android App Bundle 是一种发布格式,其中包含您应用的所有经过编译的代码和资源,它会将 APK 生成及签名交由 Google Pla ...
- 怎么从Android App Bundle (.aab)提取和转换apks文件(从AAB到APKs的转换和提取)
在Google的I/O 2018上引入了一个新的APP的发布格式,就是 Android App Bundle. 在 2019年之后,Google开始推荐开发者上传APP或者更新APP使用 .aab 格 ...
- Android App Bundle:动态功能模块
目录 Android App Bundle 创建动态功能模块 动态功能模块 与主模块建立关联 部署应用 按需分发On-Demand 免安装分发 自 2021 年 8 月起,Google Play 将开 ...
- Android之Bundle类
API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsKey ...
- Android中Bundle和Intent的区别
Bundle的作用,以及和Intent的区别: 一.Bundle: A mapping from String values to various Parcelable types 键值对的集合 类继 ...
- 1.Android简介,Android Studio安装,创建运行Android程序
文章目录 1.了解通信技术 2.Android简介 2.1 Android起源 2.2 Android历史版本 3.Android Studio开发环境的搭建 3.1 Android Stud ...
最新文章
- Error:java: 错误: 不支持发行版本 14
- hadoop生态下hive安装过程
- Nat. Biotechnol.扩增子测序革命—用16S及18S rRNA全长进行微生物多样性研究
- Windows Socket 编程_ 简单的服务器/客户端程序 .
- 阿里云ubuntu14.04下lamp环境搭建の备忘
- 前端工程化概述||模块化相关规范
- Ubuntu使用ssh公钥实现免密码登录
- Linux运维13款实用工具
- SAP 电商云 Spartacus 产品明细页面的 pageContext 如何获取的?
- 纪中A组模拟赛总结(2021.7.14)
- mysql 主从 sql线程no_Mysql 主从同步 slave_sql_running 为no
- 【0】Zookeeper QA
- 处理 ODBC, OLE DB, 和 SQL Server .NET Provider 中的异常
- Futter基础第17篇: 实现多行文本框、开关按钮、多选框、单选按钮、RadioListTile、Radio、表单
- Facebook KeyHash生成方法
- 用Excel制作甘特图并管理项目
- Java如何解决模糊查询(数据库SQL语句,报表开发工具)
- 什么时候可以用到强化学习?强化学习怎么用?
- 关于音乐制作的一些网站
- Arduino与Proteus仿真实例-简单红外寻迹小车控制仿真