安卓传值中使用Serializable和Parcelable的区别
答案:1 悬赏:70
解决时间 2021-01-21 11:32
- 提问者网友:浪荡绅士
- 2021-01-21 02:14
安卓传值中使用Serializable和Parcelable的区别
最佳答案
- 二级知识专家网友:深街酒徒
- 2021-01-21 02:48
在安卓开发中,我们经常会遇到这样一个问题,就是要在两个Activity之间传递一个实体对象,那么我们就会用到
Serializable或者Parcelable,这两个方法是用来序列化数据的,主要是用在不同的Activity之间在通过intent进行通信的
时候,数据传输时,Data部分的传输。
我们一般的在两个Activity之间传递参数是这样的,如下:
Intent it=new Intent();
Bundle bd=new Bundle();
bd.putInt("aa",20);
bd.putString("bb",ok);
it.setExtras(bd);
it.setClass(Activity1.this.class,Activity2.this.class) startActivity(it,Activity2.this.class);
就这样传递,然后在第二个Activity中接收,如下:
Bundle bd1=(Bundle)getIntent().getExtras();
String str=bd1.getString("bb");
int ii=bd1.getInt("aa");
这样,就得到了传递的数据,如果只是传递一些普通的数据,这个方法就可以了,但是如果传输的是一个自定义的结构(例如 自定义的类),或是嵌套(结构) 数据,就要进行序列化了,否则就会乱码无法解析了。比如说,自定义一个类:
public Book {
private string bookname;
private string author;
Book(string s1,string s2){bookname=s1;author=s2}
public Setbookname(string s){bookname=s;}
public Setauthor(string s){author=s;}
public string getbookname(){return bookname ; }
public string getauthor(){return author;}
}
如果要传递这个类,那么就要用到Serializable或者Parcelable了。
序列化原因:
1.永久性保存对象,保存对象的字节序列到本地文件中;
2.通过序列化对象在网络中传递对象;
3.通过序列化在进程间传递对象。
但是Serializable与Parcelable两个都是序列化,什么时候用哪个呢?
1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。
实现的时候
1.Serializable 的实现,只需要继承implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。
比如说,像下面这样:
public class Person implements Serializable {
private static final long serialVersionUID = -7060210544600464481L;
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;
}
}
2.Parcelabel 的实现,需要在类中添加一个静态成员变量 CREATOR,这个变量需要继承 Parcelable.Creator 接口。
public class Book implements Parcelable {
private String bookName;
private String author;
private int publishTime;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
}
public static final Parcelable.Creator CREATOR = new Creator() {
public Book createFromParcel(Parcel source) {
Book myBook = new Book();
myBook.bookName = source.readString();
myBook.author = source.readString();
myBook.publishTime = source.readInt();
return myBook;
}
public Book[] newArray(int size) {
return new Book[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
}
Serializable或者Parcelable,这两个方法是用来序列化数据的,主要是用在不同的Activity之间在通过intent进行通信的
时候,数据传输时,Data部分的传输。
我们一般的在两个Activity之间传递参数是这样的,如下:
Intent it=new Intent();
Bundle bd=new Bundle();
bd.putInt("aa",20);
bd.putString("bb",ok);
it.setExtras(bd);
it.setClass(Activity1.this.class,Activity2.this.class) startActivity(it,Activity2.this.class);
就这样传递,然后在第二个Activity中接收,如下:
Bundle bd1=(Bundle)getIntent().getExtras();
String str=bd1.getString("bb");
int ii=bd1.getInt("aa");
这样,就得到了传递的数据,如果只是传递一些普通的数据,这个方法就可以了,但是如果传输的是一个自定义的结构(例如 自定义的类),或是嵌套(结构) 数据,就要进行序列化了,否则就会乱码无法解析了。比如说,自定义一个类:
public Book {
private string bookname;
private string author;
Book(string s1,string s2){bookname=s1;author=s2}
public Setbookname(string s){bookname=s;}
public Setauthor(string s){author=s;}
public string getbookname(){return bookname ; }
public string getauthor(){return author;}
}
如果要传递这个类,那么就要用到Serializable或者Parcelable了。
序列化原因:
1.永久性保存对象,保存对象的字节序列到本地文件中;
2.通过序列化对象在网络中传递对象;
3.通过序列化在进程间传递对象。
但是Serializable与Parcelable两个都是序列化,什么时候用哪个呢?
1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。
实现的时候
1.Serializable 的实现,只需要继承implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。
比如说,像下面这样:
public class Person implements Serializable {
private static final long serialVersionUID = -7060210544600464481L;
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;
}
}
2.Parcelabel 的实现,需要在类中添加一个静态成员变量 CREATOR,这个变量需要继承 Parcelable.Creator 接口。
public class Book implements Parcelable {
private String bookName;
private String author;
private int publishTime;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
}
public static final Parcelable.Creator
public Book createFromParcel(Parcel source) {
Book myBook = new Book();
myBook.bookName = source.readString();
myBook.author = source.readString();
myBook.publishTime = source.readInt();
return myBook;
}
public Book[] newArray(int size) {
return new Book[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯