Android——Bundle浅析
Bundle主要用于传输数据,以key-value的形式存储数据。
- Bundle常用于在Activity间传递数据
- 当bundle传递的是对象或对象数组时,必须实现Serializable或Parcelable接口,
下面分别介绍bundle在activity间如何传递基本数据类型和对象。
传递方法:
举个例子:
写数据:
Bundle bundle=new Bundle();bundle.putString("name","police");bundle.putInt("years",8);final Intent intent=new Intent().setClassName("police.myapp","police.myapp.Main2Activity");intent.putExtras(bundle);startActivity(intent);
执行后将bundle绑定到intent,传递到Mian2Activity
读数据:
Bundle bundle=this.getIntent().getExtras();String bundleString=bundle.getString("name");int bundleInt=bundle.getInt("years");textView.setText(bundleString+bundleInt);
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 apps浅析01-Amazed:一个简单但令人上瘾的加速度为基础的大理石指导游戏。
Android apps浅析01-Amazed:一个简单但令人上瘾的加速度为基础的大理石指导游戏. 这个例子中只有4个类,一个绘制大理石类Marble,一个绘制迷宫类Maze,一个Amazed视图类, ...
- Android apps浅析01-Amazed:一个简单但令人上瘾的加速度为基础的大理石指导游戏。...
Android apps浅析01-Amazed:一个简单但令人上瘾的加速度为基础的大理石指导游戏. 这个例子中只有4个类,一个绘制大理石类Marble,一个绘制迷宫类Maze,一个Amazed视图类, ...
- Android Bundle类 学习总结
Android Bundle类 http://blog.csdn.net/randyjiawenjie/article/details/6651437 根据google官方的文档(http://dev ...
- Unable to load script from assets ‘index.android.bundle‘.
打开React-Native项目,运行之后出现标题错误.百度之后的文章都是如下: 1.首先手动在android/app/src/main目录下建立一个assets文件夹2.然后在项目的根目录下执行 r ...
- React Native之提示Unable to load script from assets ‘index.android.bundle
1 问题 Unable to load script from assets 'index.android.bundle'. Make sure your bundle is packaged cor ...
- 解决React Native unable to load script from assets index.android.bundle on windows
React Native运行的时候,经常碰到React Native unable to load script from assets index.android.bundle on windows ...
- 无法从Windows上的资产index.android.bundle加载脚本
本文翻译自:Unable to load script from assets index.android.bundle on windows I'm trying to run my first R ...
- [免费专栏] Android安全之Android加密算法浅析
也许每个人出生的时候都以为这世界都是为他一个人而存在的,当他发现自己错的时候,他便开始长大 少走了弯路,也就错过了风景,无论如何,感谢经历 转移发布平台通知:将不再在CSDN博客发布新文章,敬请移步知 ...
- Android(12)浅析 偏好设置 Preference(一)
Android(12)浅析 偏好设置 Preference(一) ### 官方基本用法:https://developer.android.google.cn/guide/topics/ui/sett ...
最新文章
- 【他们都说 select * 不好,但是 。。。】
- 电脑开机进入不了XP界面
- apache的动态编译和静态编译
- 单例(Singleton)模式
- 【视频讲解】基础实验4-2.1 树的同构 (25 分)
- 黑盒攻击的分类_「图像分类」图像分类中的对抗攻击是怎么回事?
- matlab自带kfcm函数,kfcmFun.m
- OJ1007: 鸡兔同笼
- this compilation unit is not on the build of a java project
- 安卓开发工程师应该这样快速使用机智云APP开源框架
- python服务器搭建nginx_从0开始在腾讯云服务器上搭建python3+flask+uwsgi+nginx服务器...
- VS2012下MFC程序的换肤(Skin++、SkinMagic、USkin、SkinSharp)
- TreeNMS redis/memcached可视化客户端工具的使用
- c语言三角波的mif文件,EDA课程设计报告-正弦波信号发生器的设计.doc
- 使用Android Studio生成APP图标
- 【零基础】一文读懂CPU(从二极管到超大规模集成电路)
- php验证码点击刷新,php点击可刷新验证码
- (30)tensorboard ValueError: Duplicate plugins for name projector
- android中新建文件夹在哪里,Android SdCard 新建文件夹并在文件夹中读、写文件
- Linux基金会的执行董事Jim Zemlin:开源不能没有“信任”