Use of the parcelable interface in Android Development

Source: Internet
Author: User
Use of the parcelable interface in Android Development


For Android, the transmission of complex types is mainly to convert their classes into basic byte arrays, and data passing between activities is implemented through intent. Android serialization objects can be implemented in two ways: serializable interface or parcelable interface. The serializable interface is Java
Se itself is supported, while parcelable is a feature specific to Android, which is more efficient than serializable interface implementation and can also be used in inter-process communication (IPC. The implementation of the serializable interface is very simple. Just declare it. The implementation of the parcelable interface is a little more complex, but more efficient. We recommend that you use this method to improve performance.
Role of the parcelable interface: an instance that implements the parcelable interface can write its own state information (the State information usually refers to the value of each member variable) into parcel, you can also restore the status from parcel.
Parcel is used to complete data serialization and transmission. The following describes how to implement the parcelable interface.
To serialize an object through the parcelable interface:

1. Implement the parcelable interface.
2. Implement the public interface of the parcelable Interface
Void writetoparcel (parcel DEST, int flags) method.
3. The custom type must contain a static member named creator. The member object must implement the parcelable. Creator interface and its method.
In short, you can 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 the object to the stream through writetoparcel, and read the object from the stream through createfromparcel, but you need to implement this process, therefore, the write sequence must be consistent with the read sequence.
Sample 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. // two variables are defined here to indicate that the read and write orders must be consistent.
8. Private integer ID;
9. Private string name;
10.
11. Public Person (){
12 .}
13.
14. Public Person (integer ID, string name ){
15.
16. This. ID = ID;
17. This. Name = Name;
18 .}
19.
20. Public integer GETID (){
21. Return ID;
22 .}
23.
24. Public void setid (integer ID ){
25. This. ID = ID;
26 .}
27.
28. Public String getname (){
29. Return name;
30 .}
31.
32. Public void setname (string name ){
33. This. Name = Name;
34 .}
35.
36. @ override
37. Public int describecontents (){
38. Return 0;
39 .}
40.
41. @ override
42. Public void writetoparcel (parcel DEST, int flags ){
43. // write data in javanbean to parcel. Write ID first and then name
44. DeST. writeint (this. ID );
45. DeST. writestring (this. Name );
46 .}
47.
48. // Add a static member named Creator, which implements the parcelable. Creator interface.
49. Public static final parcelable. creator <person> creator = new parcelable. creator <person> (){
50. @ override
51. Public Person createfromparcel (parcel source ){
52. // read data from parcel and return the person object
53. Return new person (source. readint (), source. readstring ());
54 .}
55.
56. @ override
57. Public person [] newarray (INT size ){
58. Return new person [size];
59 .}
60 .};
61 .}
 
Note: The parcelable interface has a lot of sub-classes in Android, as shown below:

In addition, intent also defines many get and set methods for parcelable, such:

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.

In addition, intent also implements the parcelable interface. Therefore, in Android development, parcelable is recommended as a tool to transmit replication objects.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.