The difference between Android parcelable and serializable

Source: Internet
Author: User

1) Why implement the Parfcelable interface for passing objects in intent?

A, when using memory, parcelable than serializable performance, so it is recommended to use the Parcelable class.

B, serializable generates a large number of temporary variables when serializing, resulting in frequent GC.

Note: parcelable cannot be used in situations where data is stored on disk because Parcelable does not maintain the persistence of the data in the event of a change in the outside world. Therefore, in this case, it is recommended to use serializable

1. function

Serializable's role is to save the object's properties to local files, databases, network streams, RMI for easy data transfer, which can be within a program or between two programs. The Android Parcelable is designed to be serializable inefficient, designed to transmit data efficiently between different components within the program and between different Android programs (AIDL), which only exists in memory, Parcelable is the carrier of the message through IBinder communication.

From the above design we can see the pros and cons.

2. Efficiency and Choice

Parcelable performance is better than serializable, in terms of memory overhead, so it is recommended to use parcelable when transferring data between memory, such as transfer between activity, and serializable can persist data for easy preservation. So choose serializable when you need to save or transfer data from the network, because Android versions parcelable may be different, so it is not recommended to use parcelable for data persistence

Application:

Serializable Code

 Packagecom.weidingqiang.customnetroid;Importjava.io.Serializable;/*** Created by Weidingqiang on 15/12/5.*/ Public classSdownvoImplementsSerializable {PrivateString URL; PrivateString name; Private BooleanIsdown; Private intpercent;  PublicSdownvo (String url,string name,BooleanIsdown,intpercent) {         This. Isdown =Isdown;  This. url =URL;  This. Name =name;  This. percent =percent; }     PublicString GetUrl () {returnURL; }     PublicString GetName () {returnname; }     Public BooleanIsdown () {returnIsdown; }     Public intgetpercent () {returnpercent; }}

Pass the reference

New Arraylist<sdownvo>(); List.add (newfalse, thenew  Bundle (); Bundle.putint ("Flag", 0); bundle.putserializable ("Start1", (Serializable) list); Intent.putextra ( "bundle", bundle);

Get

list<sdownvo> list = (list<sdownvo>) bundle.getserializable ("Start1"); List.size ();

Parcelable Code

 Packagecom.weidingqiang.customnetroid;ImportAndroid.os.Parcel;Importandroid.os.Parcelable;/*** Created by Weidingqiang on 15/12/5.*/ Public classPdownvoImplementsparcelable {PrivateString URL; PrivateString name; Private BooleanIsdown; Private intpercent;  PublicPdownvo () {} PublicPdownvo (String url,string name,BooleanIsdown,intpercent) {         This. Isdown =Isdown;  This. url =URL;  This. Name =name;  This. percent =percent; }     PublicString GetUrl () {returnURL; }     PublicString GetName () {returnname; }     Public BooleanIsdown () {returnIsdown; }     Public intgetpercent () {returnpercent; } @Override Public intdescribecontents () {return0; } @Override Public voidWritetoparcel (Parcel dest,intflags)        {dest.writestring (name);        dest.writestring (URL);        Dest.writeint (percent); Dest.writebyte ((byte) (Isdown? 1:0)); }     Public Static FinalParcelable.creator<pdownvo> Creator =NewCreator<pdownvo>() {        //implements the ability to create an instance of a class from source@Override Publicpdownvo Createfromparcel (Parcel source) {Pdownvo Parinfo=NewPdownvo (); Parinfo.name=source.readstring (); Parinfo.url=source.readstring (); Parinfo.percent=Source.readint (); Parinfo.isdown= (Source.readbyte ()!=0); returnParinfo; }        //Create an array of type T with a length of size@Override PublicPdownvo[] NewArray (intsize) {            return NewPdownvo[size]; }    };}

Pass the reference

New Arraylist<pdownvo>();                List1.add (newfalse, +));                 New Bundle ();                Bundle1.putint ("Flag", 1);                Bundle1.putparcelablearraylist ("Start2", List1);                Intent.putextra ("bundle", bundle1);

Get

Arraylist<pdownvo> List1 =   bundle.getparcelablearraylist ("Start2");                List1.size ();

The difference between Android parcelable and serializable

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.