Summary of Android Sequencing Learning

The main functions of serialization are:
1: Permanently save the object, save the byte sequence of the object to the local file;
2: Transfer objects through serialized objects in the network;
3: Transfer objects between processes through serialization.
2. There are two kinds of serialization in Android: Serializable and arcelable.
1: Serializable: (JavaSE itself supports) Save the properties of the object to local files, databases, network streams, rmi to facilitate data transmission.
2: Parcelable:(Android-specific features) Because Serializable is too slow, it is designed to transfer data efficiently between different components in the program and between different Android programs (AIDL), which only exists in memory.
III. Comparisons:
1: Implementation of Serializable interface is supported by JavaSE, while implementation of Parcelable interface is unique to Android. Implementing Serializable is more efficient than implementing Serializable, but its usage is also relatively complex.
2: To implement Serializable interface, we only need to mark it, and the system will automatically serialize the objects; while to implement Parcelable interface, we need to instantiate the static internal variable CREATOR, which needs our operation assistance to realize serialization.
3: When using memory, Parcelable has better performance than Serializable and less memory overhead. Therefore, it is recommended to use Parcelable for inter-memory data transmission, such as active data transmission.
4: Serializable can be implemented when saving objects permanently, so choose Serializable when you need to save or transfer data over the network. Parcelable cannot be used in cases where objects need to be stored on disk, and because different versions of android Parcelable may be different, it is not recommended to use Parcelable for data persistence.

5: Serializable produces a large number of temporary variables when serialized, which causes frequent GC and affects efficiency.

4. Parcelable interface definition:

public interface Parcelable 
{
    //Content Description Interface, Basically Neglected
    public int describeContents();
    //Write interface functions, package
    public void writeToParcel(Parcel dest, int flags);
    //The purpose of reading interface is to construct an instance processing of the class implementing Parcelable from Parcel. Because the implementation class is still unknown here, you need to use a template, inheriting class names are passed in through template parameters.
    //In order to realize the input of template parameters, the Creator embedded interface is defined, which contains two interface functions to return single and multiple instances of inherited classes, respectively.
    public interface Creator<T> 
    {
           public T createFromParcel(Parcel source);
           public T[] newArray(int size);
    }
}
5. The difference between Serializable implementation and Parcelabel implementation
1: The implementation of Serializable requires only implements Serializable. This is just marking the object and the system will serialize it automatically.
2: The implementation of Parcelabel requires not only implements Parcelabel, but also a static member variable CREATOR, which implements the Parcelable.Creator interface.
3: Implement the Parcelable step:
1)implements Parcelable
2) Rewrite the writeToParcel method to serialize your object into a Parcel object, that is, write the data of the class into the externally provided Parcel, pack the data that needs to be transferred and store it in the Parcel container to obtain the data from the Parcel container.
3) Rewrite the describeContents method, the content interface description, and return 0 by default.
4) Implementing the Parcelable.Creator interface by instantiating the static internal object CREATOR
public static final Parcelable.Creator<T> CREATOR
Note: Among them, public static final can not be less, and the name of the internal object CREATOR can not be changed. It must be all capitalized. Two methods in this interface need to be rewritten: CreateFromParcel (Parcelin) realizes reading and transferring data values from the Parcel container, encapsulates them into Parcelable objects and returns to the logical layer, and newArray(int) (size) Create an array of type T and size in one sentence, which can be used by external classes to deserialize this class array.
In short: Map your object to a Parcel object by writeToParcel, and then to your object by createFromParcel. You can also think of Parcel as a stream, writing objects into the stream through writeToParcel, and reading objects from the stream through createFromParcel, but this process requires you to implement, so the order of writing and reading must be the same.
6. Comparing the two codes:
1: Create Person class to implement 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 age)
    {
        this.age = age;
    }
}

2: Create a Book class to implement 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 in)
        {
            return new Book(in);
        }
    };
    
    public Book(Parcel in)
    {
        bookName = in.readString();
        author = in.readString();
        publishDate = in.readInt();
    }
}

Keywords: Android network less

Added by Scorptique on Thu, 20 Jun 2019 04:33:07 +0300