作为一名Android开发者,肯定对使用Intent传递数据比较熟悉,然而,Intent的putExtra()方法中所支持的数据类型是有限的,只能传递一些常用的数据类型,如String,int等,当我们项目需要我们从一个Activity中把一个对象传递到另一个Activity中,该如何实现呢?Android使用Intent实现传递对象的方式主要有两种,第一种是使用Serializable接口,第二种是使用Parcelable接口。
首先,讲第一种,Serializable是序列化的意思,表示将一个对象转换成可存储或可传输的状态,序列化后的对象可以在网络上进行传输,也可以存储到本地。
给定一个实体类Person如下:
public class Person implements Serializable{
private String name;
private int age;
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
}
</