Parcelable and serializable interface usage in Android

Source: Internet
Author: User

Turn from:

Parcelable interface usage in Android

1. parcelable Interface

Interface for classes whose instances can is written to and restored from a Parcel. Classes implementing the Parcelable interface must also has a static field called CREATOR, which is an object Implementin G The Parcelable.creator interface.

2. the implementation of parcelable is for serialization, so why Serialize?

1) Permanently save the object, save the object's byte sequence to the local file;

2) passing objects through a serialized object in the network;

3) Passing objects between processes through serialization.

3. ways to implement serialization

There are two options for implementing serialization in Android: One is to implement the Serializable interface (which is supported by the javase itself), the first is to implement the Parcelable interface (Android-specific features, efficiency than the implementation of serializable interface efficient, Can be used for intent data transfer, or for interprocess communication (IPC). The implementation of the serializable interface is very simple, declare it, and the implementation of the Parcelable interface is slightly more complex, but more efficient, it is recommended that this method to improve performance.

Note: There are two ways to pass objects intent in Android: one is bundle.putserializable (Key,object) and the other is Bundle.putparcelable (Key,object). Of course, these objects have certain conditions, the former is the implementation of the serializable interface, and the latter is the implementation of the Parcelable interface.

4. principles for choosing a serialization method

1) When using memory, parcelable performance is higher than serializable, so we recommend using Parcelable.

2) serializable generates a large number of temporary variables at serialization time, resulting in frequent GC.

3) parcelable can not be used in the case of storing data on disk, because Parcelable does not guarantee the continuity of the data in the event of external change. Although serializable is inefficient, it is recommended to use serializable at this point.

5. Application Scenarios

It is necessary to pass some data between multiple parts (activity or service) through intent, and simple types (such as numbers, strings) can be placed directly into intent. Complex types must implement the Parcelable interface.

6 , parcelable Interface Definition

Public interface Parcelable {    //Content Description interface, basic without tube public    int describecontents ();    Write interface function, package public    void Writetoparcel (Parcel dest, int flags);    Read interface, the purpose is to construct an instance processing of the class that implements the parcelable from the parcel. Because the implementation of the class is still unknown here, it is necessary to use the template way, inheriting the class name through the template parameters passed in    //in order to be able to implement the template parameters of the incoming, here is defined creator embedded interface, contains two interface functions to return single and multiple instances of inheritance class public    Interface creator<t>     {public           T createfromparcel (Parcel source);           Public t[] NewArray (int size);}    }

7 , achieve parcelable Steps

1) Implements Parcelable

2) Rewrite the Writetoparcel method to serialize your object to a parcel object, that is, to write the data of the class to an externally supplied parcel, to package the data that needs to be passed to the parcel container to save the data from the parcel container

3) Rewrite the Describecontents method, the Content interface description, the default return 0 can be

4) Instantiate the static internal object Creator Implement Interface Parcelable.creator

public static final Parcelable.creator<t> Creator

Note: the public static final one can not be less, the internal object creator name can not be changed, must all uppercase. The two methods in this interface need to be rewritten: the Createfromparcel (Parcel in) Implementation reads the passed data value from the Parcel container, encapsulates the Parcelable object to return the logical layer, and newarray (int size) creates a type of T, An array of size length, with just one sentence (return new t[size]), for the outer class to deserialize the array of this class.

In short: Map your objects into parcel objects through writetoparcel, and then map parcel objects to your objects through Createfromparcel. It is also possible to think of parcel as a stream through which the object is written to the stream through the Writetoparcel, and the object is read from the stream through the Createfromparcel, but this process needs to be done, so the order of writing and the order of reading must be consistent.

The code is as follows:


{ private int mdata;
{ return 0; }
{ out.writeint (mdata); }
{
{ return new myparcelable (in); }
{ return new myparcelable[size]; } ;
{ mdata = In.readint (); } }

8 , Serializable implementation and Parcelabel the difference between implementations

1) Serializable implementation, only need implements Serializable can. This simply marks the object and the system automatically serializes it.

2) Parcelabel implementation, not only need to implements Parcelabel, but also to add a static member variable Creator in the class, this variable needs to implement the Parcelable.creator interface.

Comparison of the two codes:

1 ) to create Person class, Implementing 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.)    {        this.age = age;    }}

2 ) to create Book class, Implementing 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.) {return new book (in);        }    };        Public book (Parcel in) {bookname = In.readstring ();        Author = in.readstring ();    Publishdate = In.readint (); }}
Category: Android Basics

Parcelable and serializable interface usage in Android

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.