首先,我们要知道Android序列化的方法有两种,一个是实现Serializable,这是JavaSE中就支持的接口,后来google推出了专门针对Android的接口Parcelable(其性能相对Serializable将近快了10倍)
然后我们要知道Android内部传递实例的基本方法:一是Bundle.putSerializable(Key,Object),另一种是Bundle.putParcelable(Key,Object),它们分别要实现Serializable和Parcelable接口。需要注意的一点是Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。这种情况还是使用Serializable比较好。
我们先看Parcelable接口的定义:
1 public interface Parcelable
2 {
3 //内容描述接口,基本不用管
4 public int describeContents();
5 //写入接口函数,打包
6 public void writeToParcel(Parcel dest, int flags);
7 //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入
8 //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例
9 public interface Creator<T>
10 {
11 public T createFromParcel(Parcel source);
12 public T[] newArray(int size);
13 }
14 public interface ClassLoaderCreator<T> extends Creator<T>
15 {
16 public T createFromParcel(Parcel source, ClassLoader loader);
17 }
18 }
继承该接口后,我们主要是重写writeToParcel()将数据序列化成一个Parcel对象
下面是将一个User类实现了Parcelable的具体源码
1 package com.catvideo.tv.catvideo.com.catvideo.user;
2
3 import android.os.Parcel;
4 import android.os.Parcelable;
5
6 /**
7 * @author lcc 957109587@qq.com
8 * @version 2016??5??31?? ????12:09:20
9 * @Description
10 */
11 public class User implements Parcelable {
12 int userId;
13 private String userName;
14 private String passWord;
15 private String email;
16 boolean state;
17
18 public User() {
19
20 }
21
22 public User(String email, String passWord) {
23 this.email = email;
24 this.passWord = passWord;
25 }
26
27 public User(int userId, String userName, String passWord, boolean state) {
28 this.userId = userId;
29 this.userName = userName;
30 this.passWord = passWord;
31 this.state = state;
32 }
33
34 public User(int userId, String userName, String passWord) {
35 this.userId = userId;
36 this.userName = userName;
37 this.passWord = passWord;
38 this.state = false;
39 }
40
41 public int getUserId() {
42 return userId;
43 }
44
45 public void setUserId(int userId) {
46 this.userId = userId;
47 }
48
49 public String getUserName() {
50 return userName;
51 }
52
53 public void setUserName(String userName) {
54 this.userName = userName;
55 }
56
57 public String getPassWord() {
58 return passWord;
59 }
60
61 public void setPassWord(String passWord) {
62 this.passWord = passWord;
63 }
64
65
66 public String getEmail() {
67 return email;
68 }
69
70 public void setEmail(String email) {
71 this.email = email;
72 }
73
74 public boolean isState() {
75 return state;
76 }
77
78 public void setState(boolean state) {
79 this.state = state;
80 }
81
82 @Override
83 public int describeContents() {
84 return 0;
85 }
86
87 @Override
88 public void writeToParcel(Parcel dest, int flags) {
89 boolean[] states = {state};
90 dest.writeString(userName);
91 dest.writeInt(getUserId());
92 dest.writeBooleanArray(states);
93 }
94
95 // public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
96 ////重写Creator
97 //
98 // @Override
99 // public User createFromParcel(Parcel source) {
100 // User user = new User();
101 //// user.userName=source.readString()
102 //
103 //// p.map = source.