Android Parcelable接口使用方法詳解
Android Parcelable接口使用方法詳解
1. Parcelable接口
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。
2.實(shí)現(xiàn)Parcelable就是為了進(jìn)行序列化,那么,為什么要序列化?
1)永久性保存對(duì)象,保存對(duì)象的字節(jié)序列到本地文件中;
2)通過序列化對(duì)象在網(wǎng)絡(luò)中傳遞對(duì)象;
3)通過序列化在進(jìn)程間傳遞對(duì)象。
3.實(shí)現(xiàn)序列化的方法
Android中實(shí)現(xiàn)序列化有兩個(gè)選擇:一是實(shí)現(xiàn)Serializable接口(是JavaSE本身就支持的),一是實(shí)現(xiàn)Parcelable接口(是android特有功能,效率比實(shí)現(xiàn)Serializable接口高效,可用于Intent數(shù)據(jù)傳遞,也可以用于進(jìn)程間通信(IPC))。實(shí)現(xiàn)Serializable接口非常簡(jiǎn)單,聲明一下就可以了,而實(shí)現(xiàn)Parcelable接口稍微復(fù)雜一些,但效率更高,推薦用這種方法提高性能。
注:Android中Intent傳遞對(duì)象有兩種方法:一是Bundle.putSerializable(Key,Object),另一種是Bundle.putParcelable(Key,Object)。當(dāng)然這些Object是有一定的條件的,前者是實(shí)現(xiàn)了Serializable接口,而后者是實(shí)現(xiàn)了Parcelable接口。
4.選擇序列化方法的原則
1)在使用內(nèi)存的時(shí)候,Parcelable比Serializable性能高,所以推薦使用Parcelable。
2)Serializable在序列化的時(shí)候會(huì)產(chǎn)生大量的臨時(shí)變量,從而引起頻繁的GC。
3)Parcelable不能使用在要將數(shù)據(jù)存儲(chǔ)在磁盤上的情況,因?yàn)镻arcelable不能很好的保證數(shù)據(jù)的持續(xù)性在外界有變化的情況下。盡管Serializable效率低點(diǎn),但此時(shí)還是建議使用Serializable 。
5.應(yīng)用場(chǎng)景
需要在多個(gè)部件(Activity或Service)之間通過Intent傳遞一些數(shù)據(jù),簡(jiǎn)單類型(如:數(shù)字、字符串)的可以直接放入Intent。復(fù)雜類型必須實(shí)現(xiàn)Parcelable接口。
6、Parcelable接口定義
public interface Parcelable
{
//內(nèi)容描述接口,基本不用管
public int describeContents();
//寫入接口函數(shù),打包
public void writeToParcel(Parcel dest, int flags);
//讀取接口,目的是要從Parcel中構(gòu)造一個(gè)實(shí)現(xiàn)了Parcelable的類的實(shí)例處理。因?yàn)閷?shí)現(xiàn)類在這里還是不可知的,所以需要用到模板的方式,繼承類名通過模板參數(shù)傳入
//為了能夠?qū)崿F(xiàn)模板參數(shù)的傳入,這里定義Creator嵌入接口,內(nèi)含兩個(gè)接口函數(shù)分別返回單個(gè)和多個(gè)繼承類實(shí)例
public interface Creator<T>
{
public T createFromParcel(Parcel source);
public T[] newArray(int size);
}
}
7、實(shí)現(xiàn)Parcelable步驟
1)implements Parcelable
2)重寫writeToParcel方法,將你的對(duì)象序列化為一個(gè)Parcel對(duì)象,即:將類的數(shù)據(jù)寫入外部提供的Parcel中,打包需要傳遞的數(shù)據(jù)到Parcel容器保存,以便從 Parcel容器獲取數(shù)據(jù)
3)重寫describeContents方法,內(nèi)容接口描述,默認(rèn)返回0就可以
4)實(shí)例化靜態(tài)內(nèi)部對(duì)象CREATOR實(shí)現(xiàn)接口Parcelable.Creator
public static final Parcelable.Creator<T> CREATOR
注:其中public static final一個(gè)都不能少,內(nèi)部對(duì)象CREATOR的名稱也不能改變,必須全部大寫。需重寫本接口中的兩個(gè)方法:createFromParcel(Parcel in) 實(shí)現(xiàn)從Parcel容器中讀取傳遞數(shù)據(jù)值,封裝成Parcelable對(duì)象返回邏輯層,newArray(int size) 創(chuàng)建一個(gè)類型為T,長(zhǎng)度為size的數(shù)組,僅一句話即可(return new T[size]),供外部類反序列化本類數(shù)組使用。
簡(jiǎn)而言之:通過writeToParcel將你的對(duì)象映射成Parcel對(duì)象,再通過createFromParcel將Parcel對(duì)象映射成你的對(duì)象。也可以將Parcel看成是一個(gè)流,通過writeToParcel把對(duì)象寫到流里面,在通過createFromParcel從流里讀取對(duì)象,只不過這個(gè)過程需要你來實(shí)現(xiàn),因此寫的順序和讀的順序必須一致。
代碼如下:
public class MyParcelable implements Parcelable
{
private int mData;
public int describeContents()
{
return 0;
}
public void writeToParcel(Parcel out, int flags)
{
out.writeInt(mData);
}
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];
}
};
private MyParcelable(Parcel in)
{
mData = in.readInt();
}
}
8、Serializable實(shí)現(xiàn)與Parcelabel實(shí)現(xiàn)的區(qū)別
1)Serializable的實(shí)現(xiàn),只需要implements Serializable 即可。這只是給對(duì)象打了一個(gè)標(biāo)記,系統(tǒng)會(huì)自動(dòng)將其序列化。
2)Parcelabel的實(shí)現(xiàn),不僅需要implements Parcelabel,還需要在類中添加一個(gè)靜態(tài)成員變量CREATOR,這個(gè)變量需要實(shí)現(xiàn) Parcelable.Creator 接口。
兩者代碼比較:
1)創(chuàng)建Person類,實(shí)現(xiàn)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)創(chuàng)建Book類,實(shí)現(xiàn)Parcelable
public class Book implements Parcelable
{
private String bookName;
private String author;
private int publishDate;
public Book()
{
}
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 getPublishDate()
{
return publishDate;
}
public void setPublishDate(int publishDate)
{
this.publishDate = publishDate;
}
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags)
{
out.writeString(bookName);
out.writeString(author);
out.writeInt(publishDate);
}
public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>()
{
@Override
public Book[] newArray(int size)
{
return new Book[size];
}
@Override
public Book createFromParcel(Parcel in)
{
return new Book(in);
}
};
public Book(Parcel in)
{
bookName = in.readString();
author = in.readString();
publishDate = in.readInt();
}
}
以上就是 Android Parcelable接口的使用方法,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android序列化實(shí)現(xiàn)接口Serializable與Parcelable詳解
- Android中Parcelable的使用詳解
- Android序列化之Parcelable和Serializable的使用詳解
- 在Android Studio中Parcelable插件的簡(jiǎn)單使用教程
- Android Parcelable與Serializable詳解及區(qū)別
- 很詳細(xì)的android序列化過程Parcelable
- Android中的Parcelable序列化對(duì)象
- Android中Parcelable的作用實(shí)例解析
- Android Parcleable接口的調(diào)用源碼層分析
相關(guān)文章
android編程開發(fā)之全屏和退出全屏的實(shí)現(xiàn)方法
這篇文章主要介紹了android編程開發(fā)之全屏和退出全屏的實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了Android全屏及退出全屏的頁(yè)面布局與功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android應(yīng)用更新之自動(dòng)檢測(cè)版本及自動(dòng)升級(jí)
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用更新之自動(dòng)檢測(cè)版本及自動(dòng)升級(jí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Android AutoWrapTextView中英文排版問題的解決方法
這篇文章主要給大家介紹了關(guān)于Android AutoWrapTextView中英文排版問題的解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05
Android WebView實(shí)現(xiàn)截長(zhǎng)圖功能
這篇文章主要為大家詳細(xì)介紹了Android截長(zhǎng)圖的一種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

