Parcelable

1、概念

Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface。
android提供了一種新的類型:parcel(中文解釋:包裹,小包),本類用來封裝數據的容器,封裝后的數據可以通過Intent或IPC傳遞,除了基本類型外,只有實現了Parcelable接口的類才能放入parcel中。parcel一般都用在Binder通信,通過read和write方法進行客戶端與服務端的數據傳遞(通信)。
比如:frameworks層服務端與hardware客戶端的Binder通信
reply->writeInt32(getCardReaderSize());
int mid = data.readInt32();
用來存放parcel數據的是內存(RAM),而不是永久介質(Nand等)。
parcelable定義了把數據寫入parcel和從parcel讀出數據的接口,一個類的實例,如果需要封裝到消息中去,就必須實現這一接口,如果實現了這個接口,該類的實例就是可以“被打包”。

2、為什么要實現Parcelable接口?

實現Parcelable就是為了進行序列化,那么,為什么要序列化?

  1. 永久性保存對象,保存對象的字節序列到本地文件中;
  2. 通過序列化對象在網絡中傳遞對象;
  3. 通過序列化在進程間傳遞對象。

3、實現序列化的方法

Android中實現序列化有兩個選擇:一是實現Serializable接口(是JavaSE本身就支持的),一是實現Parcelable接口(是Android特有功能,效率比實現Serializable接口高效,可用于Intent數據傳遞,也可以用于進程間通信(IPC))。實現Serializable接口非常簡單,聲明一下就可以了,而實現Parcelable接口稍微復雜一些,但效率更高,推薦用這種方法提高性能。
注:Android中Intent傳遞對象有兩種方法:一是Bundle.putSerializable(Key,Object),另一種是Bundle.putParcelable(Key,Object)。當然這些Object是有一定的條件的,前者是實現了Serializable接口,而后者是實現了Parcelable接口。

4、選擇序列化方法的原則

  1. 在使用內存的時候,Parcelable比Serializable性能高,所以推薦使用Parcelable;
  2. Serializable在序列化的時候會產生大量的臨時變量,從而引起頻繁的GC;
  3. Parcelable不能使用在要將數據存儲在磁盤上的情況,因為Parcelable不能很好的保證數據的持續性在外界有變化的情況下。盡管Serializable效率低點,但此時還是建議使用Serializable 。

5、應用場景

需要在多個部件(Activity或Service)之間通過Intent傳遞一些數據,簡單類型(如:數字、字符串)的可以直接放入Intent。復雜類型必須實現Parcelable接口。

6、Parcelable接口定義

public interface Parcelable 
{
    //內容描述接口,可以不做任何操作,直接return 0即可;
    public int describeContents();
    //寫入接口函數,打包
    public void writeToParcel(Parcel dest, int flags);
    //讀取接口,目的是要從Parcel中構造一個實現了Parcelable的類的實例處理。因為實現類在這里還是不可知的,所以需要用到模板的方式,繼承類名通過模板參數傳入
    //為了能夠實現模板參數的傳入,這里定義Creator嵌入接口,內含兩個接口函數分別返回單個和多個繼承類實例
    public interface Creator<T> 
    {
           public T createFromParcel(Parcel source);
           public T[] newArray(int size);
    }
}

7、實現Parcelable步驟

  1. implements Parcelable;
  2. 重寫describeContents方法,內容接口描述,默認返回0就可以;
  3. 重寫writeToParcel方法,將你的對象序列化為一個Parcel對象,即:將類的數據寫入外部提供的Parcel中,打包需要傳遞的數 據到Parcel容器保存,以便從 Parcel容器獲取數據;
  4. 實例化靜態內部對象CREATOR實現接口Parcelable.Creator:
public static final Parcelable.Creator<T> CREATOR

注:其中public static final一個都不能少,內部對象CREATOR的名稱也不能改變,必須全部大寫。需重寫本接口中的兩個方法:createFromParcel(Parcel in) 實現從Parcel容器中讀取傳遞數據值,封裝成Parcelable對象返回邏輯層,newArray(int size) 創建一個類型為T,長度為size的數組,僅一句話即可(return new T[size]),供外部類反序列化本類數組使用。
總之:通過writeToParcel將你的對象映射成Parcel對象,再通過createFromParcel將Parcel對象映射成你的對象。也可以將Parcel看成是一個流,通過writeToParcel把對象寫到流里面,在通過createFromParcel從流里讀取對象,只不過這個過程需要你來實現,因此寫的順序和讀的順序必須一致。

8、Serializable實現與Parcelabel實現的區別

  1. Serializable的實現,只需要implements Serializable 即可。這只是給對象打了一個標記,系統會自動將其序列化。
  2. Parcelabel的實現,不僅需要implements Parcelabel,還需要在類中添加一個靜態成員變量CREATOR,這個變量需要實現 Parcelable.Creator 接口。

9、實現Parcelable接口代碼

import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;
public class ContentNavInfoBean implements Parcelable{

    private String navId;
    private String navName;
    private String navType;
    private String orderNum;
    private String lessonNum;
    public String getNavId() {
        return navId;
    }
    public void setNavId(String navId) {
        this.navId = navId;
    }
    public String getNavName() {
        return navName;
    }
    public void setNavName(String navName) {
        this.navName = navName;
    }
    public String getNavType() {
        return navType;
    }
    public void setNavType(String navType) {
        this.navType = navType;
    }
    public String getOrderNum() {
        return orderNum;
    }
    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }
    public String getLessonNum() {
        return lessonNum;
    }
    public void setLessonNum(String lessonNum) {
        this.lessonNum = lessonNum;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(navId);
        dest.writeString(navName);
        dest.writeString(navType);
        dest.writeString(orderNum);
        dest.writeString(lessonNum);        
    }
    
    public static final Parcelable.Creator<ContentNavInfoBean> CREATOR = new Parcelable.Creator<ContentNavInfoBean>() 
             {
                 public ContentNavInfoBean createFromParcel(Parcel in) 
                 {
                     ContentNavInfoBean infoBean = new ContentNavInfoBean();
                    infoBean.navId=in.readString();
                    infoBean.navName=in.readString();
                    infoBean.navType=in.readString();
                    infoBean.orderNum=in.readString();
                    infoBean.lessonNum=in.readString();
                     
                     return infoBean;
                 }
                 public ContentNavInfoBean[] newArray(int size) 
                 {
                     return new ContentNavInfoBean[size];
                 }
             };
}

10、Intent參數傳遞序列化對象

Intent intent = new Intent(A.class,B.class);
Bundle bundle = new Bundle();
bundle.putParcelable("contentNavInfo", contentNavInfo);
intent.putExtras(bundle);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容