For Android, the transfer of complex types, mainly the conversion of their own classes to the underlying byte array, the activity between the data passed through intent implementation. There are two main ways to implement an serializable interface, or to implement an Parcelable interface, for Android serialization objects. Implementing the Serializable interface is Java
The SE itself is supported, and Parcelable is an Android-specific feature that is more efficient than implementing the serializable interface and can also be used in interprocess communication (IPC). Implementation of the Serializable interface is very simple, declare it. The implementation of the Parcelable interface is slightly more complex, but more efficient, and it is recommended to improve performance in this way.
Parcelable interface: an instance that implements the Parcelable interface can write its own state information (state information usually refers to the value of each member variable) to parcel, or restore its state from parcel.
Parcel is used to complete the serialization of data transfer. Here's a look at how to implement the Parcelable interface.
To serialize an object by implementing the Parcelable interface:
1, realize parcelable interface.
2, and implement the public Parcelable interface
void Writetoparcel (Parcel dest, int flags) method.
3. A custom type must contain a static member named Creator, which requires the implementation of the Parcelable.creator interface and its methods.
In short: Map your objects into parcel objects through writetoparcel, and then map parcel objects to your objects through Createfromparcel. It is also possible to think of parcel as a stream through which the object is written to the stream through the Writetoparcel, and the object is read from the stream through the Createfromparcel, but this process needs to be done, so the order of writing and the order of reading must be consistent.
Example code:
[Java]
1. Package com.yang.domain;
2.
3. Import Android.os.Parcel;
4. Import android.os.Parcelable;
5.
6. Public class Person implements Parcelable {
7.//The two variables are defined here to show that the order of reading and writing is consistent
8. Private Integer ID;
9. Private String name;
10.
One. Public person () {
12.}
13.
Public person (Integer ID, String name) {
15.
This.id = ID;
THIS.name = name;
18.}
19.
Public Integer getId () {
. return ID;
22.}
23.
public void SetId (Integer id) {
This.id = ID;
26.}
27.
. Public String GetName () {
. return name;
30.}
31.
. public void SetName (String name) {
THIS.name = name;
34.}
35.
@Override.
PNS. public int describecontents () {
. return 0;
39.}
40.
@Override.
public void Writetoparcel (Parcel dest, int flags) {
43.//write the data in the Javanbean to parcel. Write the ID first and then write the name
Dest.writeint (this.id);
Dest.writestring (this.name);
46.}
47.
48.//Add a static member named Creator, which implements the Parcelable.creator interface
. public static final parcelable.creator<person> Creator = new Parcelable.creator<person> () {
@Override
Wuyi Public Person Createfromparcel (Parcel source) {
52.//Read data from parcel, return person object
. return new Person (Source.readint (), source.readstring ());
54.}
55.
@Override.
person[public] NewArray (int size) {
[+]. return new person[size];
59.}
60.};
61.}
Note: The Parcelable interface has very many subcategories in Android, as follows:
And intent also defines a lot of get, set methods for parcelable, such as:
Intent |
PutExtra (String name, parcelable value) ADD extended data to the intent. |
<t extends Parcelable> T |
Getparcelableextra (String name) Retrieve extended data from the intent. |
And intent itself implements the Parcelable interface, so it is highly recommended to parcelable as a tool for copying objects in Android development.
Use of Parcelable interface in Android development