Detailed explanation of Android Parcelable interface usage

Detailed explanation of Android Parcelable interface usage

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 Parcelable.Creator interface。

2. The purpose of implementing Parcelable is serialization, so why serialization?

1) Permanently save the object, and save the byte sequence of the object to the local file; 2) Transmitting objects in the network by serializing objects; 3) Passing objects between processes through serialization.

3. Method of serialization

There are two options for serialization in Android: one is to implement the serializable interface (supported by javase itself), and the other is to implement the Parcelable interface (a unique function of Android, which is more efficient than the serializable interface, and can be used for intent data transmission or inter process communication (IPC)). The implementation of the serializable interface is very simple. It's OK to declare it. The implementation of the Parcelable interface is slightly more complex, but it is more efficient. This method is recommended to improve performance.

Note: in Android, there are two methods for intent to transfer objects: one is bundle.putserializable (key, object), and the other is bundle.putparcelable (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 selecting serialization methods

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, resulting in frequent GC. 3) Parcelable cannot be used when the data is to be stored on disk, because Parcelable cannot guarantee the continuity of the data in case of external changes. Although serializable efficiency is low, it is recommended to use serializable at this time.

5. Application scenarios

Some data needs to be transferred between multiple components (activities or services) through intent. Simple types (such as numbers and strings) can be directly put into intent. Complex types must implement the Parcelable interface.

6. Parcelable interface definition

7. Implement Parcelable steps

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 parcel provided externally, package the data to be transferred to the parcel container and save it, so as to obtain data from the parcel container 3) Rewrite the describecontents method, content interface description, You can return 0 by default. 4) instantiate the static internal object creator to implement the interface parcelable.creator

Note: there must be no less public static final, and the name of the internal object creator cannot be changed. It must be capitalized. Two methods in this interface need to be rewritten: createfromparcel (parcel in) implements reading the passed data value from the parcel container, encapsulates it into a Parcelable object, and returns to the logic layer. Newarray (int size) creates an array of type T and length size in just one sentence (return new t [size]) for the external class to deserialize the array of this class.

In short: map your object to a parcel object through writetoparcel, and then map the parcel object to your object through createfromparcel. You can also regard parcel as a stream. Write objects to the stream through writetoparcel and read objects from the stream through createfromparcel. However, this process needs to be implemented by you. Therefore, the writing order and reading order must be consistent.

The code is as follows:

8. The difference between serializable implementation and parcelabel implementation

1) For the implementation of serializable, you only need implements serializable. This just marks the object and the system will automatically serialize it.

2) The implementation of parcelabel requires not only implements parcelabel, but also a static member variable creator in the class, which needs to implement the parcelable.creator interface.

Comparison of two codes:

1) Create a person class to implement serializable

2) Create a Book class to implement Parcelable

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 for communication and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>