1. What is the use of Parcelable and Serializable and what is the difference between them?

Function:

Serializable is a serialization interface provided by Java. It is an empty interface that provides standard serialization and deserialization operations for objects. It is used to save object properties to local files, databases, network streams, and RMIS for easy data transfer, either within an application or between applications.

Android’s Parcelable is designed to be Serializable too slow. It is designed to efficiently transfer data between different components within an application and between different Android applications (AIDL), which only exists in memory. Parcelable is the carrier of messages communicated through IBinder.

The difference between:

1) When using memory, Parcelable is better than Serializable, so Parcelable is recommended. 2) Serializable generates a large number of temporary variables when serialized, leading to frequent GC. 3) Parcelable cannot be used when data is to be stored on disk, because Parcelable does not have a good guarantee of data continuity in the case of external changes. Although Serializable efficiency is low, it is recommended to use Serializable at this time.

2. What is the general process for customizing a class to implement Parcelable?

Custom entity class PersonBean, implement Parcelable interface

1). Override writeToParcel serializes the object data into a Parcel object (which becomes a Parcel object after serialization). For the Parcel container to fetch the data, flags flags have two values: 0 or 1. A value of 1 indicates that the current object needs to be returned as a return value and cannot be freed immediately, that is, PARCELABLE_WRITE_RETURN_VALUE, but in almost all cases returns 0.)

2). Replicate the describeContents method with a default value of 0 (return the memory description of the current object). Return 1 if it has a file descriptor, CONTENTS_FILE_DESCRIPTOR, 0 otherwise, 0 in almost all cases)

3). Instantiate static internal object CREATOR to implement interface Parcelable. CREATOR.

Example:

public class PersonBean implements Parcelable { private String name; private String age; // Create the original object protected PersonBean(Parcel) from the serialized objectin) { name = in.readString(); age = in.readString(); Public static final Creator<PersonBean> Creator = new Creator<PersonBean>() {// Create the original object from the serialized object @override public PersonBean createFromParcel(Parcelin) {
            return new PersonBean(in); Override public PersonBean[] newArray(int size) {returnnew PersonBean[size]; }}; // Returns the memory description of the current object. Return 1 (CONTENTS_FILE_DESCRIPTOR) if it contains a file descriptor, or 0 otherwise // in almost all cases 0 @override public intdescribeContents() {
        return0; } // Write the current object to the serialization structure, where flags flags have two values: 0 or 1 // 1 indicates that the current object needs to be returned as the return value and cannot be released immediately. Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(age); }}Copy the code