在Android中通过Intent使用Bundle传递对象的使用方法
作者:Snail 时间:2023-04-23 10:15:38
Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。
被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io.Serializable和android.os.Parcelable
Java中使用的是Serializable,而谷歌在Android使用了自定义的Parcelable。
两种序列化方式的区别:
1.在使用内存的时候,Parcelable比Serializable性能高,推荐使用Parcelable类;
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC;
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下,
这种情况建议使用Serializable。
第一步:定义序列化实体类;
Serializable方式:
/**
* PersonSer
*
* @author http://blog.csdn.net/zuolongsnail
*/
public class PersonSer implements Serializable {
/**
* serialVersionUID的作用是在修改实体类后,可以正常的序列化和反序列化,在此不多说,大家可以谷歌百度下。
*/
private static final long serialVersionUID = -7620435178023928252L;
private String name;
private int 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;
}
}
Parcelable方式:
/**
* PersonPar
*
* @author http://blog.csdn.net/zuolongsnail
*/
public class PersonPar implements Parcelable {
private String name;
private int 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;
}
/**
* 序列化实体类
*/
public static final Parcelable.Creator<PersonPar> CREATOR = new Creator<PersonPar>() {
public PersonPar createFromParcel(Parcel source) {
PersonPar personPar = new PersonPar();
personPar.name = source.readString();
personPar.age = source.readInt();
return personPar;
}
public PersonPar[] newArray(int size) {
return new PersonPar[size];
}
};
@Override
public int describeContents() {
return 0;
}
/**
* 将实体类数据写入Parcel
*/
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(name);
parcel.writeInt(age);
}
}
第二步:传递序列化对象;
Intent intent = new Intent(MainActivity.this, DestActivity.class);
Bundle bundle = new Bundle();
switch (v.getId()) {
case R.id.serializable_btn:
// Serializable传递对象
PersonSer personSer = new PersonSer();
personSer.setName("zuolong");
personSer.setAge(26);
bundle.putSerializable(SER_KEY, personSer);
intent.putExtra("type", SER_TYPE);
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.parcelable_btn:
// Parcelable传递对象
PersonPar personPar = new PersonPar();
personPar.setName("snail");
personPar.setAge(27);
// Bundle有putParcelableArray和putParcelableArrayList方法,也就可以传递数组和列表
bundle.putParcelable(PAR_KEY, personPar);
intent.putExtra("type", PAR_TYPE);
// Intent也有putParcelableArrayListExtra方法,可以传递实现Parcelable接口的对象列表
intent.putExtras(bundle);
startActivity(intent);
break;
}
第三步:获取对象;
StringBuffer sb = new StringBuffer();
String type = getIntent().getStringExtra("type");
// 根据type判断是哪种类型
if (type.equals(MainActivity.SER_TYPE)) {
// 获取Serializable对象
PersonSer personSer = (PersonSer) getIntent().getSerializableExtra(
MainActivity.SER_KEY);
sb.append("----From Serializable----").append("\n");
sb.append("Name:").append(personSer.getName()).append("\n");
sb.append("Age:").append(personSer.getAge()).append("\n");
} else if (type.equals(MainActivity.PAR_TYPE)) {
// 获取Parcelable对象
PersonPar personPar = (PersonPar) getIntent().getParcelableExtra(
MainActivity.PAR_KEY);
sb.append("----From Parcelable----").append("\n");
sb.append("Name:").append(personPar.getName()).append("\n");
sb.append("Age:").append(personPar.getAge()).append("\n");
}
附源码:源码下载
标签:bundle,Android
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Mybatis中的 ${ } 和 #{ }的区别使用详解
2021-07-02 22:35:27
C#创建windows系统用户的方法
2022-07-16 20:30:12
android编程实现对话框的封装实例
2022-02-12 12:58:54
详解Spring Boot集成MyBatis(注解方式)
2023-10-03 17:45:47
![](https://img.aspxhome.com/file/2023/1/64481_0s.jpg)
Android列表组件ListView使用详解之动态加载或修改列表数据
2023-10-01 16:14:38
![](https://img.aspxhome.com/file/2023/5/137715_0s.png)
C#时间格式转换为时间戳的方法步骤
2022-04-13 12:27:19
Android Studio3.6设置Gradle Offline Mode的方法
2022-07-26 09:36:01
![](https://img.aspxhome.com/file/2023/0/111700_0s.png)
C#生成带logo的二维码
2021-11-03 17:38:41
![](https://img.aspxhome.com/file/2023/0/79090_0s.png)
使用Deflate算法对文件进行压缩与解压缩的方法详解
2022-01-27 09:48:49
基于spring boot 的配置参考大全(推荐)
2021-11-28 23:32:41
C#利用DesignSurface如何实现简单的窗体设计器
2023-10-18 18:43:28
![](https://img.aspxhome.com/file/2023/9/126099_0s.png)
在项目中直接使用hystrix的流程分析
2021-10-22 01:55:21
![](https://img.aspxhome.com/file/2023/8/73048_0s.jpg)
c#对象反序列化与对象序列化示例详解
2022-08-19 16:32:54
![](https://img.aspxhome.com/file/2023/1/106471_0s.jpg)
Hashmap非线程安全关于hash值冲突处理
2023-11-11 09:22:10
C#中RSA加密与解密的实例详解
2022-09-01 15:43:06
![](https://img.aspxhome.com/file/2023/2/111152_0s.jpg)
创建动态代理对象bean,并动态注入到spring容器中的操作
2021-09-04 01:02:43
![](https://img.aspxhome.com/file/2023/9/71589_0s.jpg)
Spring计时器StopWatch使用示例
2023-05-16 21:23:02
C#结束进程及子进程
2021-06-22 15:18:10
Spring+EHcache缓存实例详解
2023-12-12 20:27:51
![](https://img.aspxhome.com/file/2023/2/77932_0s.jpg)
C#加密解密类实例程序
2022-01-25 18:17:09