Parcelable Serialized Objects

Source: Internet
Author: User


The purpose of the serialization
persist the object, save the object's byte sequence to the local file,
to pass the object in the network by serializing the object,
pass the object between processes through serialization, and
Pass the complex custom class object in intent. The Parcelable interface needs to be implemented.

Two. Methods for implementing serialization and comparison of advantages and disadvantages
1. Implement the Serializable interface, do not need a real-specific interface, the equivalent of a tag object, the system will automatically serialize it.
Pros: You can store data on disk
Disadvantages: In an Android system, serialization generates a large number of temporary variables that cause frequent GC.
2. Implementing the Parcelable interface, which is unique to Android, needs to be implemented according to certain specifications. The
Parcelable interface maps objects to parcel objects through the Writetoparcel method, and Createfromparcel objects to specific objects by means of the Creator method of the inner class object.
where Writetoparcel and Createfromparcel must be implemented, and the order of reading and writing must be consistent.
Advantage: Operating in memory, high performance.
Cons: There is no good guarantee of the persistence of data when there is a change in the outside world, so it is not possible to save data to disk in this way.

Three. The Parcelable interface uses the method
to implement the Parcelable interface, which implements the following methods
public int describecontents ()
public void Writetoparcel ( Parcel dest, int flags)
assumes the new class is T, implements the constructor method
Public T (Parcel in)
defines a static inner object Creator implements the interface Parcelable.creator, where public Static final can not be absent, Creator all caps cannot be changed. The format is as follows
public static final parcelable.creator<t> Creator
instance,
Public Static final parcelable.creator<t> Creator = new creator<t> () {

@Override
Public t[] NewArray (int size) {
return new T[size];
}

@Override
Public T Createfromparcel (Parcel source) {
return new T (source);
}
};
The Ps:createfromparcel method implements reading data from the parcel and encapsulates the Parcelable object back into the logical layer.
NewArray returns an array of type T that is used by the outer deserialization of this class array.

Four. Code examples

ImportAndroid.os.Parcel;Importandroid.os.Parcelable; Public classPersonImplementsparcelable {Private LongmId; Private intMAge; PrivateString Mname; PrivateString maddress;  PublicObject Mextra;  PublicPerson (LongIdintAge , string name, String Address, Object extra) {MId=ID; MAge=Age ; Mname=name; Maddress=Address; Mextra=Extra; }     PublicPerson (Parcel in) {mId=In.readlong (); MAge=In.readint (); Mname=in.readstring (); if(In.readbyte () = = 1) {Mextra=in.readparcelable (GetClass (). getClassLoader ()); } maddress=in.readstring (); } @Override Public intdescribecontents () {return0; } @Override Public voidWritetoparcel (Parcel dest,intflags)        {Dest.writelong (mId);        Dest.writeint (MAge);        Dest.writestring (Mname); Parcelable P=NULL; if(Mextra! =NULL) {            Try{p=(parcelable) Mextra; } Catch(ClassCastException e) {p=NULL; }        }        if(P! =NULL) {Dest.writebyte (byte) 1);        Dest.writeparcelable (P, flags); } Else{dest.writebyte (byte) 0);    } dest.writestring (maddress); }     Public Static FinalParcelable.creator<person> Creator =NewCreator<person>() {@Override PublicPerson[] NewArray (intsize) {            return NewPerson[size]; } @Override PublicPerson createfromparcel (Parcel source) {return NewPerson (source);   }    };} 






Parcelable Serialized Objects

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.