Android 序列化Parcelable的使用詳解
背景:
在Java虛擬機(jī)中,對象的傳遞稱為數(shù)據(jù)傳遞不可或缺的一部分,但如果一旦虛擬機(jī)停止工作,該對象在內(nèi)存中也就被釋放,地址空間不存在,對象自然就不能再被重復(fù)利用,如果我們想持久使用這個(gè)對象怎么辦?寫到文件中?存數(shù)據(jù)庫?最好的方法就是能夠保存下來,把一個(gè)對象的空間地址以及屬性保存下來,在Java中已提供了一個(gè)接口Serializable。
1.Serializable是Java的JDK提供的,由于該序列化不夠絲滑,在PC等大型操作系統(tǒng)中使用,體驗(yàn)不出來,如果在Adnroid 虛擬機(jī)中,數(shù)據(jù)的傳遞存在一定的差異,這時(shí),google官方提供了新的序列化對象Parcelable
Parcelable相比較Serializable要復(fù)雜的很多,Serializable序列化只要對象繼承該接口,即可。但是Parcelable需要我們手動(dòng)去分裝
Parcelable序列化的封裝
說明:
如果最外層Bean需要實(shí)現(xiàn)Parcelable,那么內(nèi)部的變量也是一個(gè)類對象,也要實(shí)現(xiàn)序列化,可以先從變量類開始序列化,最后實(shí)現(xiàn)外層bean的序列化
序列化之前先把變量定義好,這樣用助于后面序列化的操作
1.IDE自動(dòng)封裝
1.1先將當(dāng)前類繼承接口Parcelable
1.2將鼠標(biāo)懸停在錯(cuò)誤提示位置,這個(gè)時(shí)候IDE提示如下

1.3 我們只需要點(diǎn)擊AddimplementationParcelable
1.4IDE會(huì)自動(dòng)完成組裝,自動(dòng)讀寫變量
結(jié)果如下:
public class MyParcelBean implements Parcelable {
private int age;
private String name;
private boolean sex;
protected MyParcelBean(Parcel in) {
age = in.readInt();
name = in.readString();
sex = in.readByte() != 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
dest.writeByte((byte) (sex ? 1 : 0));
}
@Override
public int describeContents() {
return 0;
}
public static final Creator CREATOR = new Creator() {
@Override
public MyParcelBean createFromParcel(Parcel in) {
return new MyParcelBean(in);
}
@Override
public MyParcelBean[] newArray(int size) {
return new MyParcelBean[size];
}
};
}
**************************************************************************************************
2.手動(dòng)封裝
手動(dòng)封裝我們需要知道,處理哪些東西,這些東西是什么。流程是什么
同樣已MyParcelBean類為例,
2.1.類的變量定義好了,然后繼承接口Parcelable
2.2.重寫以下方法
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(age);
dest.writeString(name);
dest.writeByte((byte) (sex ? 1 : 0));
}
writeToParcel是我們需要處理的地方,這邊提供了兩個(gè)參數(shù):Parcel 和flags
Parcel :是變量的封裝,
flags:是標(biāo)識(shí)
接下來我們重點(diǎn)講解Parcel
1.Parcel 主要提供數(shù)據(jù)的讀寫,如果你是什么類型,在讀寫過程就用什么類型,但是boolean除外,
2.boolean類型再Parcel中,沒有該類型,通過通過byte類型替代
3.讀寫的順序必須對應(yīng),如下:
dest.writeInt(age);
dest.writeString(name);
dest.writeByte((byte) (sex ? 1 : 0));
存的順序是這樣,那么讀的順序也一定要這樣,否則數(shù)據(jù)會(huì)異常
protected MyParaceBean(Parcel in) {
age = in.readInt();
name = in.readString();
sex = in.readByte() != 0;
}
3.每個(gè)parcelable接口都有一個(gè)造物者,我們還要實(shí)現(xiàn)這個(gè)固定寫法:
public static final Creator CREATOR = new Creator() {
@Override
public MyParaceBean createFromParcel(Parcel in) {
return new MyParaceBean(in);
}
@Override
public MyParaceBean[] newArray(int size) {
return new MyParaceBean[size];
}
};
這是固定寫法
4.我們需要實(shí)現(xiàn)一個(gè)保護(hù)類型的構(gòu)造器,用來讀在這數(shù)據(jù)
protected MyParaceBean(Parcel in) {
age = in.readInt();
name = in.readString();
sex = in.readByte() != 0;
}
5.關(guān)于Parcel write寫有些要注意的地方
5.1對象的保存
如果保存一個(gè)對象,這個(gè)對象必須也要實(shí)現(xiàn)parcelable接口
在Bean中
private MyParaceBeanChild child;
寫:
dest.writeParcelable(child, flags);
讀:
child = in.readParcelable(MyParaceBeanChild.class.getClassLoader());
5.2數(shù)組數(shù)據(jù)的保存
List的數(shù)據(jù)保存,也是需要在List泛型對象中先實(shí)現(xiàn)parcelable的接口,
在Bena中定義如下:
private List
寫:
dest.writeTypedList(list);
讀:
list = in.createTypedArrayList(MyParaceBeanChild.CREATOR);
5.3Boolean類型
類型不支持,通過byte來復(fù)寫,可以參考上面boolean說法
一般在讀寫類型中,Parcel數(shù)據(jù)封裝提供了對應(yīng)的絕大多數(shù)類型。
總結(jié):
核心點(diǎn):
1.構(gòu)造器讀數(shù)據(jù)
protected MyParaceBean(Parcel in) { }
2.重寫方法:寫數(shù)據(jù)
@Override
public void writeToParcel(Parcel dest, int flags) { }
3.造物標(biāo)識(shí):CREATOR
public static final Creator CREATOR = new Creator() {
@Override
public MyParaceBean createFromParcel(Parcel in) {
return new MyParaceBean(in);
}
@Override
public MyParaceBean[] newArray(int size) {
return new MyParaceBean[size];
}
};
只需要處理好這三個(gè)地方,其他都是自己的Java邏輯。
本文僅代表作者觀點(diǎn),版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請?jiān)谖闹凶⒚鱽碓醇白髡呙帧?/p>
免責(zé)聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請及時(shí)與我們聯(lián)系進(jìn)行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com





