Use of the Parcelable interface in Android, androidparcelable

Source: Internet
Author: User

Use of the Parcelable interface in Android, androidparcelable

Serialization is very common during development. For example, you want to save the object to a local disk or transmit it over the network. There are two methods to implement serialization: one is to implement the Serializable interface, and the other is to implement Parcelable.

Differences between Serializable and Parcelable

1. Serializable is the interface provided by JDK, while Parcelable is provided by Android SDK.

2. Serializable serialization is based on disks, while Parcelable is based on memory. The read/write efficiency in the memory must be higher than that on the disk. Therefore, Parcelable is used to transmit objects across processes in Android.

Parcelable Interface Definition

1 public interface Parcelable {2 // content description interface, basically no need to worry about 3 public int describeContents (); 4 // write interface function, package 5 public void writeToParcel (Parcel dest, int flags); 6 // read interface, which is used to construct an instance that implements Parcelable from Parcel. Because the implementation class is unknown here, you need to use the template method. The inherited class name is passed in through the template parameter. 7 // in order to implement template parameter input, the Creator embedded interface is defined here, which contains two interface functions that return one and multiple inherited class instances respectively. 8 public interface Creator <T> {9 public T createFromParcel (Parcel source); 10 public T [] newArray (int size); 11}

From the parcelable interface definition, we can see that to implement the parcelable interface, we need to implement the following methods:

1. describeContents method. Content interface description. By default, 0 is returned;

2. writeToParcel method. This method writes the data of the class to the external provided Parcel. That is, package the data to be transferred to the Parcel container for storage, so that the data can be obtained from the parcel container. This method is declared as follows:

For details about writeToParcel (Parcel dest, int flags), see javadoc.

3. Static Parcelable. Creator interface. This interface has two methods:

CreateFromParcel (Parcel in) reads the passed data value from the Parcel container and encapsulates it into a Parcelable object and returns the logic layer.

NewArray (int size) creates an array of T type and size. It can only be a single sentence (return new T [size. The method is used for external class deserialization of this class array.

 

Use of Parcelable

1 public class AppContent implements Serializable, Parcelable {2 // Application name 3 private String name; 4 // application download link 5 private String url; 6 7 private int downloadPercent = 0; 8 9 private Status status = Status. PENDING; 10 11 public AppContent (String name, String url) {12 this. name = name; 13 this. url = url; 14} 15 16 public String getName () {17 return name; 18} 19 20 public void setName (String name) {21 this. name = name; 22} 23 24 public String getUrl () {25 return url; 26} 27 28 public void setUrl (String url) {29 this. url = url; 30} 31 32 public int getDownloadPercent () {33 return downloadPercent; 34} 35 36 public void setDownloadPercent (int downloadPercent) {37 this. downloadPercent = downloadPercent; 38} 39 40 public Status getStatus () {41 return status; 42} 43 44 public void setStatus (Status status Status) {45 this. status = status; 46} 47 48 @ Override49 public String toString () {50 return name; 51} 52 53 @ Override54 public int describeContents () {55 return 0; 56} 57 58 // The implemented Parcel interface must overwrite the implemented method 59 @ Override60 public void writeToParcel (Parcel dest, int flags) {61/* write the AppContent member to Parcel, 62 * Note: Data in Parcel is written and read in order, that is, the data first written is read 63 */64 dest. writeString (name); 65 dest. writeString (url); 66 dest. writeInt (downloadPercent); 67 dest. writeValue (status); 68} 69 70 // This static domain is required and must be named CREATOR. Otherwise, 71 public static final Parcelable will occur. creator <AppContent> CREATOR = 72 new Parcelable. creator <AppContent> () {73 74 @ Override75 public AppContent createFromParcel (Parcel source) {76 // read the related member information of AppContent written by using the writeToParcel method from Parcel 77 String name = source. readString (); 78 String url = source. readString (); 79 int downloadPercent = source. readInt (); 80 Status status Status = (Status) source. readValue (new ClassLoader () {}); 81 AppContent appContent = new AppContent (name, url); 82 appContent. setDownloadPercent (downloadPercent); 83 appContent. setStatus (status); 84 // For more read information, create and return the Person object 85 return appContent; 86} 87 88 @ Override89 public AppContent [] newArray (int size) 90 {91 // TODO Auto-generated method stub92 // return the AppContent object array 93 return new AppContent [size]; 94} 95}; 96 97}

Pass through Intent:

1    Intent intent = new Intent(Constants.DOWNLOAD_MSG);2    Bundle bundle = new Bundle();3    bundle.putParcelable("appContent", appContent);4    intent.putExtras(bundle);

 

For more information, see http://www.tuicool.com/articles/mjzazn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.