Why does intent inherit the parseable or serialized interface when passing data, and does not implement any methods

1) Permanently save the object and save the byte sequence of the object to the local file;

2) Passing objects in the network by serializing them;

3) Passing objects between processes by serialization

 

There are two options to implement serialization in Android: one is to implement the Serializable interface (supported by Java se itself), the other is to implement the Parcelable interface (a unique function of Android, which is more efficient than the implementation of the Serializable interface, which can be used for Intent data transmission, or for interprocedural communication (IPC)). It's very simple to implement the Serializable interface, just declare it, but it's a little more complicated to implement the Parcelable interface, but it's more efficient. This method is recommended to improve performance.

Note: in Android, there are two ways to pass an Intent object: one is to Bundle.putSerializable(Key, object), the other is Bundle.putParcelable(Key,Object). Of course, there are certain conditions for these objects. The former implements the Serializable interface, and the latter implements the Parcelable interface.

 

1) When using memory, Parcelable has higher performance than Serializable, so it is recommended to use Parcelable.

2) Serializable generates a large number of temporary variables during serialization, which causes frequent GC.

3) Parcelable can't be used when data is to be stored on disk, because Parcelable can't guarantee the continuity of data in case of external changes. Although Serializable is inefficient, it is recommended to use Serializable at this time.

 

Difference: 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;
    }
}

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();
    }

Tags: Android network Java

Posted on Sat, 27 Jun 2020 03:51:13 -0400 by noon