溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android Parcelable和Serializable的區(qū)別

發(fā)布時(shí)間:2020-07-31 12:30:16 來源:網(wǎng)絡(luò) 閱讀:224 作者:Android丶VG 欄目:移動(dòng)開發(fā)

本文主要介紹ParcelableSerializable的作用、效率、區(qū)別及選擇。

1、作用

Serializable的作用是為了保存對(duì)象的屬性到本地文件、數(shù)據(jù)庫(kù)、網(wǎng)絡(luò)流、rmi以方
便數(shù)據(jù)傳輸,當(dāng)然這種傳輸可以是程序內(nèi)的也可以是兩個(gè)程序間的。而Android的
Parcelable的設(shè)計(jì)初衷是因?yàn)?code>Serializable效率過慢,為了在程序內(nèi)不同組件間以及
不同Android程序間(AIDL)高效的傳輸數(shù)據(jù)而設(shè)計(jì),這些數(shù)據(jù)僅在內(nèi)存中存在,
Parcelable是通過IBinder通信的消息的載體。

從上面的設(shè)計(jì)上我們就可以看出優(yōu)劣了

2、效率及選擇

Parcelable的性能比Serializable好,在內(nèi)存開銷方面較小,所以在內(nèi)存間數(shù)據(jù)傳輸
時(shí)推薦使用Parcelable,如activity間傳輸數(shù)據(jù),而Serializable可將數(shù)據(jù)持久化方便
保存,所以在需要保存或網(wǎng)絡(luò)傳輸數(shù)據(jù)時(shí)選擇Serializable,因?yàn)閍ndroid不同版本
Parcelable可能不同,所以不推薦使用Parcelable進(jìn)行數(shù)據(jù)持久化。

3、編程實(shí)現(xiàn)

對(duì)于Serializable,類只需要實(shí)現(xiàn)Serializable接口,并提供一個(gè)序列化版本
id(serialVersionUID)即可。而Parcelable則需要實(shí)現(xiàn)writeToParcel、
describeContents函數(shù)以及靜態(tài)的CREATOR變量,實(shí)際上就是將如何打包和解包
的工作自己來定義,而序列化的這些操作完全由底層實(shí)現(xiàn)。

Parcelable的一個(gè)實(shí)現(xiàn)例子如下

  public class MyParcelable implements Parcelable {
     private int mData;
     private String mStr;

     public int describeContents() {
        return 0;
     }

     // 寫數(shù)據(jù)進(jìn)行保存
     public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
        out.writeString(mStr);
     }

     // 用來創(chuàng)建自定義的Parcelable的對(duì)象
     public static final Parcelable.Creator<MyParcelable> CREATOR= new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
           return new MyParcelable(in);
        }

        public MyParcelable[] newArray(int size) {
           return new MyParcelable[size];
        }
     };

     // 讀數(shù)據(jù)進(jìn)行恢復(fù)
     private MyParcelable(Parcel in) {
        mData = in.readInt();
        mStr = in.readString();
     }
  }

從上面我們可以看出Parcel的寫入和讀出順序是一致的。如果元素是list讀出時(shí)需要
先new一個(gè)ArrayList傳入,否則會(huì)報(bào)空指針異常。如下:

  list = new ArrayList<String>();
  in.readStringList(list);

PS: 在自己使用時(shí),read數(shù)據(jù)時(shí)誤將前面int數(shù)據(jù)當(dāng)作long讀出,結(jié)果后面的順序錯(cuò)亂,報(bào)如下異常,當(dāng)類字段較多時(shí)務(wù)必保持寫入和讀取的類型及順序一致。

  12-21 20:14:10.317: E/AndroidRuntime(21114): Caused by: java.lan
g.RuntimeException: Parcel android.os.Parcel@4126ed60: Unmarshal
ling unknown type code 3014773 at offset 164
4、高級(jí)功能上

Serializable序列化不保存靜態(tài)變量,可以使用Transient關(guān)鍵字對(duì)部分字段不進(jìn)行序
列化,也可以覆蓋writeObject、readObject方法以實(shí)現(xiàn)序列化過程自定義。

更多內(nèi)容詳情請(qǐng)關(guān)注我的GitHub:https://github.com/xiangjiana/Android-MS

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI