Android中Parcelable介面用法

來源:互聯網
上載者:User

標籤:

from:

http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

 

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。

 1 public interface Parcelable { 2     public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001; 3     public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001; 4     public int describeContents();//返回0即可
    //一個是把本對象(實現了Parcelable的對象)的資料寫入Parcel中
5 public void writeToParcel(Parcel dest, int flags); 6   /** 7 * Interface that must be implemented and provided as a public CREATOR 8 * field that generates instances of your Parcelable class from a Parcel. 9 */10 public interface Creator<T> {//提供一個內部類,供還原序列化的時候把Parcel對象轉化為對象,即本對象11 /**12 * Create a new instance of the Parcelable class, instantiating it13 * from the given Parcel whose data had previously been written by14 * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.15 * 16 * @param source The Parcel to read the object‘s data from.17 * @return Returns a new instance of the Parcelable class.18 */19 public T createFromParcel(Parcel source);//讀出source中的資料給T(即本對象)20 21 /**22 * Create a new array of the Parcelable class.23 * 24 * @param size Size of the array.25 * @return Returns an array of the Parcelable class, with every entry26 * initialized to null.27 */28 public T[] newArray(int size);29 }30 public interface ClassLoaderCreator<T> extends Creator<T> {31 public T createFromParcel(Parcel source, ClassLoader loader);32 }33 }
2.序列化的原因

1)永久性儲存對象,儲存對象的位元組序列到本地檔案中;

2)通過序列化對象在網路中傳遞對象;

3)通過序列化在進程間傳遞對象。

 

3.Parcelable與Serializable的對比
  • Parcelable

  1.android特有;

  2.實現複雜;

  3.比Serializable高效

  4.可以用於處理序間通訊(IPC),也可用於intent通訊;

  5.不能使用在要將資料存放區在磁碟上的情況,因為Parcelable不能很好的保證資料的持久性在外界有變化的情況下。

  6.複雜類型必須實現Parcelable介面。

  • Serializable

  1.java SE實現;

  2.在序列化的時候會產生大量的臨時變數,從而引起頻繁的GC。

  3.實現簡單

 Parcel是公用的記憶體空間,所以可以存、取資料。由於是記憶體操作,所以要比Serialize利用外部存放裝置來存、取資料要快!

如果要發送對象最簡單的方法是: 把要發的資料直接當做對象發送,接收也不用Bundle(這是可以的),因為已經預設建立。

4.public static final一個都不能少,內部對象CREATOR的名稱也不能改變,必須全部大寫。

簡而言之:通過writeToParcel將你的對象映射成Parcel對象,再通過createFromParcel將Parcel對象映射成你的對象。

5.簡單例子
 1 public class Person implements Parcelable { 2     private String name; 3     private int age; 4     private static final String TAG = "Person"; 5  6     public String getName() { 7         return name; 8     } 9 10     public void setName(final String name) {11         this.name = name;12     }13 14     public int getAge() {15         return age;16     }17 18     public void setAge(final int age) {19         this.age = age;20     }21 22     public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {23         @Override24         public Person createFromParcel(final Parcel source) {25             Log.d(TAG, "createFromParcel");26             Person mPerson = new Person();27             mPerson.name = source.readString();28             mPerson.age = source.readInt();29             return mPerson;30         }31 32         @Override33         public Person[] newArray(final int size) {34             // TODO Auto-generated method stub35             return new Person[size];36         }37     };38 39     @Override40     public int describeContents() {41         // TODO Auto-generated method stub42         Log.d(TAG, "describeContents");43         return 0;44     }45 46     @Override47     public void writeToParcel(final Parcel dest, final int flags) {48         // TODO Auto-generated method stub49         Log.d(TAG, "writeToParcel");50         dest.writeString(name);51         dest.writeInt(age);52     }53 }

 

6.writeToParcel()寫和createFromParcel()讀對象的資料們的順序必須一致;



Android中Parcelable介面用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.