Detailed Serializable and Parcelable

Sequence: In order to save the state of various objects in memory, and can save the state of the object read out

Andrews achieve serialization has two interfaces, one Serializable, one parcelable.

First, to achieve serialization:

  1, the object can be permanently stored on disk

  2, the objects can be transferred in the network

  3, the object is to pass in the process.
Second, comparison of serializable and parcelable
  1, parcelable serializable slightly more complex than
  2, more parcelable efficiency, better performance
Third, the application scenario
  1, a complex type
  2, are not suitable for simple data processing, such as the string
  3, is not suitable for a serialized object case on disk

A, Seralizable description:

1.Serializable is java serialization technology, the easiest way is to use the increased need for serialization class implements Serializable, and adds a unique sequence of id:

private static final long serialVersionUID = 1L;

The default mode is preferably set directly 1L, because the Java SDK automatically hash calculation, and generates a unique UID value. Will get recalculated automatically set after the change in the member variable; manually set serialVersionUID benefit is that if you change the current class member variables, such as adding or deleting later, the UID does not change, then deserialization will not fail the new UID, resulting in failure. However, in most cases it can be both.

2.Seralizable relatively Parcelable, the advantage is very simple, just need to perform a sequence of class can be a class, you do not need to manually handle the serialization and deserialization process, so often used in data processing network requests, Activity using the values transferred between.

FIG transfer between Activiyt:

3.Seralizable static variables can not be serialized, using transient modification of the object can not be serialized.

4. When the parent class implements a serialization, subclasses automatically serialized and do not need to display implements Serializable.

Two, Parcelable description:

1.Parcelable android is unique serialization API, which appears to solve the serious Serializable consumption of resources in the process of serialization problems, but because of their own need for manual processing using serialization and de-serialization process, will be tied to specific code, the use of more complicated, generally only gets used when the data memory.

2. Parcelable dependent on the Parcel, Parcel packaging means, to achieve the principle is based on a shared data block in memory, serialization and deserialization of data that are operating in a so achieved.

3.Parcelable of three processes: serialization, deserialization and description

public  class MyParcelable the implements a Parcelable {
      Private  int MDATA;
      Private String MSTR;
    // Description: Returns the value of 0 or CONTENTS_FILE_DESCRIPTOR (i.e. 1), if writeToParcel (Parcel dest, int flags) comprising an output file descriptor, else return 0, 
// almost all cases are under return of 0. the
public int describeContents () { return 0 ; } // write data stored, serialized, out represents a serialized object to be written. There are two values 0 and flags PARCELABLE_WRITE_RETURN_VALUE (i.e. 1), almost all the cases are of 0. The public void writeToParcel (Parcel OUT, int flags) { out.writeInt (MDATA); out.writeString (MSTR); } // objects Parcelable used to create custom deserialization, must contain a non-null static realized Parcelable.Creator CREATOR interface public static Final Parcelable.Creator <MyParcelable> CREATOR = new new Parcelable.Creator <MyParcelable> () { public MyParcelable createFromParcel (Parcel in) { return new new MyParcelable (in); } public MyParcelable [] newArray ( int size) { return new new MyParcelable [size]; } }; // read data recovery private MyParcelable (Parcel in) { mData = in.readInt(); mStr = in.readString(); } }

From the above we can see that the order of writing and reading is consistent Parcel. If you need new element is read out a list ArrayList passed, otherwise it will report null pointer exception. as follows:

list = new ArrayList<String>();
in.readStringList(list);

When the type field MUST be consistent with the type of read and write and the order of more time.


Description describecontents:
set to 0 in general, particularly for the

This is the case when the requirements for the file descriptor (descriptor) to be serialized.

 

Attachment:

 

Parcel is a container for packaging various types of data. After data Parcel can communicate with the server IPC client data exchanged between blinder process.

 

Parcelable: the subject is a can Parcel interface. To Parcelable custom data structures can be packaged, it must implement Parcelable method.


----------------
Disclaimer: This article is CSDN blogger original article "hacker_crazy", following the undefined copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/hacker_crazy/article/details/80840868

Guess you like

Origin www.cnblogs.com/yz123/p/12034756.html