Detailed explanation of how to use the Android Parcelable interface
1. Parcelable interface
Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the interface。
2. Implementing Parcelable is for serialization, so why serialize it?
1) Save the object permanently and save the byte sequence of the object to the local file;
2) Pass objects in the network by serializing them;
3) Pass objects between processes through serialization.
3. Methods to implement serialization
There are two options for implementing serialization in Android: one is to implement the Serializable interface (which is supported by JavaSE itself), and the other is to implement the Parcelable interface (which is a unique function of Android, and is more efficient than implementing the Serializable interface. It can be used for Intent data transmission and can also be used for inter-process communication (IPC)). Implementing the Serializable interface is very simple, just declare it, while implementing the Parcelable interface is a little more complicated, but it is more efficient. It is recommended to use this method to improve performance.
Note: There are two ways to pass objects in Intent in Android: one is (Key, Object), and the other is (Key, Object). Of course, these Objects have certain conditions. The former implements the Serializable interface, while the latter implements the Parcelable interface.
4. Principles for choosing a serialization method
1) When using memory, Parcelable has higher performance than Serializable, so it is recommended to use Parcelable.
2) Serializable will generate a large number of temporary variables during serialization, thereby causing frequent GC.
3) Parcelable cannot be used when data is stored on disk, because Parcelable cannot ensure the persistence of data when there are changes in the outside world. Although Serializable is inefficient, it is recommended to use Serializable at this time.
5. Application scenarios
It is necessary to pass some data through an Intent between multiple components (Activity or Service). Simple types (such as numbers, strings) can be directly placed into the Intent. Complex types must implement the Parcelable interface.
6. Parcelable interface definition
public interface Parcelable { //Content description interface, basically nothing to worry about public int describeContents(); //Write interface function, package public void writeToParcel(Parcel dest, int flags); //Read the interface, the purpose is to construct an instance of a class that implements Parcel from Parcel to process. Because the implementation class is still agnostic here, it is necessary to use the template method, and the inherited class name is passed in through the template parameters. //In order to implement the incoming of template parameters, here is a Creator embedding interface, which contains two interface functions that return a single and multiple inherited class instances respectively. public interface Creator<T> { public T createFromParcel(Parcel source); public T[] newArray(int size); } }
7. Steps to implement Parcelable
1)implements Parcelable
2) Rewrite the writeToParcel method and serialize your object into a Parcel object, that is, write the data of the class into the externally provided Parcel, and package the data that needs to be passed to the Parcel container to save in order to obtain the data from the Parcel container.
3) Rewrite the describeContents method, the content interface description, and the default return 0 is enough
4) Instantiate the static internal object CREATOR to implement the interface
public static final <T> CREATOR
Note:Among them, public static final cannot be missing, and the name of the internal object CREATOR cannot be changed, and must be fully capitalized. Two methods in this interface need to be rewrite: createFromParcel(Parcel in) implements reading and passing data values from the Parcel container, encapsulating them into a Parcelable object to return to the logical layer, newArray(int size) creates an array of type T and length size, just one sentence (return new T[size]), for external classes to deserialize the array of this class.
in short: Map your object into a Parcel object through writeToParcel, and then map the Parcel object into your object through createFromParcel. You can also regard Parcel as a stream, write the object into the stream through writeToParcel, and read the object from the stream through createFromParcel. However, this process requires you to implement it, so the order of writing and reading must be consistent.
The code is as follows:
public class MyParcelable implements Parcelable { private int mData; public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { (mData); } public static final <MyParcelable> CREATOR = new <MyParcelable>() { public MyParcelable createFromParcel(Parcel in) { return new MyParcelable(in); } public MyParcelable[] newArray(int size) { return new MyParcelable[size]; } }; private MyParcelable(Parcel in) { mData = (); } }
8. The difference between Serializable implementation and Parcelabel implementation
1) The implementation of Serializable only requires implements Serializable. This just marks the object and the system will automatically serialize it.
2) The implementation of Parcelabel not only requires implements Parcelabel, but also requires adding a static member variable CREATOR to the class. This variable needs to implement an interface.
Comparison of the two codes:
1) Create Person class and 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) { = name; } public int getAge() { return age; } public void setAge(int age) { = age; } }
2) Create Book class and 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) { = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { = author; } public int getPublishDate() { return publishDate; } public void setPublishDate(int publishDate) { = publishDate; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel out, int flags) { (bookName); (author); (publishDate); } public static final <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 = (); author = (); publishDate = (); } }
The above is how to use the Android Parcelable interface. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!