一、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相关推荐

  1. Android App Bundle 和Unity AAB BundleTools

    1.为什么要用Android App Bundle 从 2021年8月起,新应用需要使用 Android App Bundle 才能在 Google Play 中发布.现在,Play Feature ...

  2. Android App Bundle混淆加密加壳加固保护的解决方案(过Google App上架审核)

    Android AAB简介和AAB包格式 AAB即Android App Bundle,是Google官方发布的一种新的App包格式,可以有效缩减App大小,提升用户安装和更新App的体验.在Goog ...

  3. Android App Bundle

    1. Android App Bundle 是什么? 从 2021 年 8 月起,新应用需要使用 Android App Bundle 才能在 Google Play 中发布. Android App ...

  4. 分享篇 - 58同城基于Android APP Bundle开发的全新编译模式(编译速度提升70%)

    目录 1. Wafers 项目背景 2. 效果展示 3. 实现方案 4. 改造期间遇到的问题 5. 如何接入使用 6. 对比 Instant Run 和 Apply Changes 7. 总结 1. ...

  5. Android App Bundle打包发布GooglePlay

    Android App Bundle 简介官方文档 Android App Bundle 是一种发布格式,其中包含您应用的所有经过编译的代码和资源,它会将 APK 生成及签名交由 Google Pla ...

  6. 怎么从Android App Bundle (.aab)提取和转换apks文件(从AAB到APKs的转换和提取)

    在Google的I/O 2018上引入了一个新的APP的发布格式,就是 Android App Bundle. 在 2019年之后,Google开始推荐开发者上传APP或者更新APP使用 .aab 格 ...

  7. Android App Bundle:动态功能模块

    目录 Android App Bundle 创建动态功能模块 动态功能模块 与主模块建立关联 部署应用 按需分发On-Demand 免安装分发 自 2021 年 8 月起,Google Play 将开 ...

  8. Android之Bundle类

    API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsKey ...

  9. Android中Bundle和Intent的区别

    Bundle的作用,以及和Intent的区别: 一.Bundle: A mapping from String values to various Parcelable types 键值对的集合 类继 ...

  10. 1.Android简介,Android Studio安装,创建运行Android程序

    文章目录 1.了解通信技术 2.Android简介   2.1 Android起源   2.2 Android历史版本 3.Android Studio开发环境的搭建 3.1 Android Stud ...

最新文章

  1. 半透明遮罩层覆盖整个可视区域
  2. SSO单点登录基于CAS架构封装 Memcached 实例
  3. 2021年了,该拥有自己的深度学习框架了
  4. 活久见!技术面试官竟然给我出了一个脑筋急转弯,还问我王者荣耀什么段位?...
  5. 2013-11-5 深圳尚游网络公司 - 服务器开发工程师
  6. 静态时序分析——多周期、半周期和伪路径
  7. linux c之STDIN_FILENO的作用及与stdin的区别
  8. excel利用countif/match/lookup函数对比分析数据
  9. android thread线程通讯
  10. Navicat for MySQL 64位破解版
  11. python给批量图片添加文字_Python之利用PIL批量给图片添加文字
  12. java-Effectively final
  13. vivo s12参数
  14. 85.You want to configure and schedule offline database backups to run automatically. Which tool or u
  15. 黑龙江移动新魔百盒M411A_2+8_S905L3A_线刷固件包
  16. Linux:安装 telnet 命令
  17. Raspberry PI 编译WLan驱动模块, 并配置登录WIFI
  18. CCF A类与B类的国际期刊(高性能计算、人工智能)
  19. 如何在前端完美控制浏览器兼容性问题
  20. texstudio深色主题设置

热门文章

  1. PageHelper PageInfo 手动List分页
  2. python selenium爬取QQ空间说说
  3. PHP开发的资产管理系统源码基于layuimini框架
  4. 使用python获取股票“业绩快报总资产、净资产”等“上市公司业绩快报”数据
  5. 分布式服务框架——Dubbo
  6. 计算机语言python读音_python+拼音
  7. ansys如何删除线_workbench 怎么取消剖面?
  8. 模板文件下载后乱码或异常的可能原因及解决方案
  9. 重金属行业经销商渠道管理系统:完善客户管理体系,规范渠道销售管理
  10. 挂科一门就被退学!国科大考试就是这么刺激!亲眼所见大神6 个小时弄懂 600 多页的书!...