Android serialization object interface Parcelable usage method, androidparcelable

Source: Internet
Author: User

Android serialization object interface Parcelable usage method, androidparcelable

What is Parcelable?

Parcelable defines the interfaces for writing data to Parcel and reading data from Parcel. An object (represented by a class). To encapsulate it in a message, you must implement this interface to implement this interface, this entity becomes "packable.

Parcelable passed object

Android serialization objects can be implemented in two ways:

1. Implement the Serializable interface, which is supported by JavaSE itself;

2. Implement the Parcelable interface. Parcelable is a feature specific to Android and is more efficient than Serializable. It is also supported for Intent data transmission and can also be used for inter-process communication (IPC ),

Except for the basic type, only the classes that implement the Parcelable interface can be put into Parcel.

Parcelable Interface Definition

[Java]View plaincopyprint?
  1. Public interface Parcelable {
  2. // Content description interface.
  3. Public int describeContents ();
  4. // Write interface function, package
  5. Public void writeToParcel (Parcel dest, int flags );
  6. // Read interface, which is used to construct an instance that implements Parcelable from Parcel. Because the implementation class is unknown here, you need to use the template method. The inherited class name is passed in through the template parameter.
  7. // To implement the input of template parameters, the Creator embedded interface is defined here, which contains two interface functions that return one and multiple inherited class instances respectively.
  8. Public interface Creator <T> {
  9. Public T createFromParcel (Parcel source );
  10. Public T [] newArray (int size );
  11. }

Implement the Parcelable interface?

From the parcelable interface definition, we can see that to implement the parcelable interface, we need to implement the following methods:

1. describeContents method. Content interface description. By default, 0 is returned;

2. writeToParcel method. This method writes the data of the class to the external provided Parcel. That is, package the data to be transferred to the Parcel container for storage, so that the data can be obtained from the parcel container. This method is declared as follows:

For details about writeToParcel (Parcel dest, int flags), see javadoc.

3. Static Parcelable. Creator interface. This interface has two methods:

CreateFromParcel (Parcel in) reads the passed data value from the Parcel container and encapsulates it into a Parcelable object and returns the logic layer.

NewArray (int size) creates an array of T type and size. It can only be a single sentence (return new T [size. The method is used for external class deserialization of this class array.

Code Implementation

1. encapsulate data and pass the Person object implementing the parcelable interface to TwoActivity;

[Java]View plaincopyprint?
  1. Public class DemoActivity extends Activity {
  2. /** Called when the activity is first created .*/
  3. @ Override
  4. Public void onCreate (Bundle savedInstanceState ){
  5. Super. onCreate (savedInstanceState );
  6. SetContentView (R. layout. main );
  7. // Encapsulate data
  8. Person p = new Person ();
  9. P. setId (1 );
  10. P. setName ("xiaoming ");
  11. // Use Intent to pass the Person object
  12. Intent I = new Intent (this, TwoActivity. class );
  13. I. putExtra ("Person", p );
  14. StartActivity (I );
  15. }
  16. }
2. TwoActivity obtains data, parses the Person object passed by DemoActivity, and prints the data;
[Java]View plaincopyprint?
  1. Public class TwoActivity extends Activity {
  2. @ Override
  3. Protected void onCreate (Bundle savedInstanceState ){
  4. // TODO Auto-generated method stub
  5. Super. onCreate (savedInstanceState );
  6. Person p = (Person) getIntent (). getParcelableExtra ("Person ");
  7. System. out. println ("p_id" + p. getId ());
  8. System. out. println ("p_name" + p. getName ());
  9. }
  10. }

3. Implementation of the parcelable Interface

[Java]View plaincopyprint?
  1. Public class Person implements Parcelable {
  2. // Member variable
  3. Private int id;
  4. Private String name;
  5. // 1. You must implement the Parcelable. Creator interface. Otherwise, an error is returned when you obtain the Person data, as shown below:
  6. // Android. OS. BadParcelableException:
  7. // Parcelable protocol requires a Parcelable. Creator object called CREATOR on class com. um. demo. Person
  8. // 2. This interface reads the Person data from the Percel container and returns the Person object to the logic layer.
  9. // 3. Implement the Parcelable. Creator interface object name must be CREATOR. It is better to report the error mentioned above;
  10. // 4. When reading data in the Parcel container, the data must be read in the Order stated by the member variables. Otherwise, an error occurs when obtaining data.
  11. // 5. deserialization object
  12. Public static final Parcelable. Creator <Person> CREATOR = new Creator (){
  13. @ Override
  14. Public Person createFromParcel (Parcel source ){
  15. // TODO Auto-generated method stub
  16. // The data must be read in the order declared by the member variables. Otherwise, an error occurs when obtaining data.
  17. Person p = new Person ();
  18. P. setId (source. readInt ());
  19. P. setName (source. readString ());
  20. Return p;
  21. }
  22. @ Override
  23. Public Person [] newArray (int size ){
  24. // TODO Auto-generated method stub
  25. Return new Person [size];
  26. }
  27. };
  28. Public int getId (){
  29. Return id;
  30. }
  31. Public void setId (int id ){
  32. This. id = id;
  33. }
  34. Public String getName (){
  35. Return name;
  36. }
  37. Public void setName (String name ){
  38. This. name = name;
  39. }
  40. @ Override
  41. Public int describeContents (){
  42. // TODO Auto-generated method stub
  43. Return 0;
  44. }
  45. @ Override
  46. Public void writeToParcel (Parcel dest, int flags ){
  47. // TODO Auto-generated method stub
  48. // 1. Data must be encapsulated in the order declared by the member variables. Otherwise, an error occurs when data is obtained.
  49. // 2. serialize the object
  50. Dest. writeInt (id );
  51. Dest. writeString (name );
  52. }
  53. }

Related Article

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.