Parcelable interface for data transfer in Android

Source: Internet
Author: User
Tags int size requires serialization java se

For Android, passing complex types is primarily about converting its own class into a base byte array, and passing data between the activity is accomplished through intent. There are two main methods of the Android serialization object, which is to implement the serializable interface, or implement the Parcelable interface. The implementation of the serializable interface is supported by the Java SE itself, while Parcelable is an Android-specific feature that is more efficient than the serializable interface and can be used in interprocess communication (IPC). The implementation of the serializable interface is very simple, a statement can be. The implementation of the Parcelable interface is slightly more complex, but more efficient, the recommended way to improve performance.

The function of the Parcelable interface: an instance that implements the Parcelable interface can write its state information (the state information usually refers to the value of each member variable) to Parcel, or it can recover its state from Parcel.

Parcel is used to complete the serialization of data. Here is a description of the implementation of the Parcelable interface method.

To serialize an object by implementing the Parcelable interface:

1, realize parcelable interface.

2, and realize the Parcelable interface of public

void Writetoparcel (Parcel dest, int flags) method

3. A custom type must contain a static member named Creator, which requires the implementation of the Parcelable.creator interface and its methods.

In short: Map your objects into parcel objects by writetoparcel, and then map parcel objects to your objects by Createfromparcel. You can also think of parcel as a stream, writetoparcel the object into the stream, reading the object from the stream through the Createfromparcel, but this process requires you to implement it, so the order of writing and the order of reading must be the same.

NewArray (int size) creates an array of type T, with a length of size, and only one sentence (return new t[size])

Sample code:

 import Android.os.Parcel;   
        
Import android.os.Parcelable;   
     public class Person implements Parcelable {//here defines two variables to indicate that the order of read and write is identical to the private Integer ID;   
        
     private String name;  Public person () {} public person (Integer ID, String name) {this.id   
         = ID;   
     THIS.name = name;   
     Public Integer GetId () {return id;   
     The public void SetId (Integer id) {this.id = ID;   
     Public String GetName () {return name;   
     public void SetName (String name) {this.name = name;   
     @Override public int describecontents () {return 0; @Override public void Writetoparcel (Parcel dest, int flags) {//write the data in Javanbean to parce L   
First write the ID and then write the name Dest.writeint (this.id);         Dest.writestring (this.name); //Add a static member, named Creator, that implements the Parcelable.creator interface public static final Parcelable.creator<per son> CREATOR = new parcelable.creator<person> () {@Override Public person Createfromparcel P Arcel Source {//reads data from parcel, returns the person object return to new person (Source.readint (), Source.rea   
         Dstring ());   
         @Override public person[] NewArray (int size) {return new person[size];   
 }   
     }; }

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.