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?
- Public interface Parcelable {
- // Content description interface.
- Public int describeContents ();
- // Write interface function, package
- Public void writeToParcel (Parcel dest, int flags );
- // 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.
- // 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.
- Public interface Creator <T> {
- Public T createFromParcel (Parcel source );
- Public T [] newArray (int size );
- }
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?
- Public class DemoActivity extends Activity {
- /** Called when the activity is first created .*/
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- // Encapsulate data
- Person p = new Person ();
- P. setId (1 );
- P. setName ("xiaoming ");
- // Use Intent to pass the Person object
- Intent I = new Intent (this, TwoActivity. class );
- I. putExtra ("Person", p );
- StartActivity (I );
- }
- }
2. TwoActivity obtains data, parses the Person object passed by DemoActivity, and prints the data;
[Java]View plaincopyprint?
- Public class TwoActivity extends Activity {
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- // TODO Auto-generated method stub
- Super. onCreate (savedInstanceState );
- Person p = (Person) getIntent (). getParcelableExtra ("Person ");
- System. out. println ("p_id" + p. getId ());
- System. out. println ("p_name" + p. getName ());
- }
- }
3. Implementation of the parcelable Interface
[Java]View plaincopyprint?
- Public class Person implements Parcelable {
- // Member variable
- Private int id;
- Private String name;
- // 1. You must implement the Parcelable. Creator interface. Otherwise, an error is returned when you obtain the Person data, as shown below:
- // Android. OS. BadParcelableException:
- // Parcelable protocol requires a Parcelable. Creator object called CREATOR on class com. um. demo. Person
- // 2. This interface reads the Person data from the Percel container and returns the Person object to the logic layer.
- // 3. Implement the Parcelable. Creator interface object name must be CREATOR. It is better to report the error mentioned above;
- // 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.
- // 5. deserialization object
- Public static final Parcelable. Creator <Person> CREATOR = new Creator (){
- @ Override
- Public Person createFromParcel (Parcel source ){
- // TODO Auto-generated method stub
- // The data must be read in the order declared by the member variables. Otherwise, an error occurs when obtaining data.
- Person p = new Person ();
- P. setId (source. readInt ());
- P. setName (source. readString ());
- Return p;
- }
- @ Override
- Public Person [] newArray (int size ){
- // TODO Auto-generated method stub
- Return new Person [size];
- }
- };
- Public int getId (){
- Return id;
- }
- Public void setId (int id ){
- This. id = id;
- }
- Public String getName (){
- Return name;
- }
- Public void setName (String name ){
- This. name = name;
- }
- @ Override
- Public int describeContents (){
- // TODO Auto-generated method stub
- Return 0;
- }
- @ Override
- Public void writeToParcel (Parcel dest, int flags ){
- // TODO Auto-generated method stub
- // 1. Data must be encapsulated in the order declared by the member variables. Otherwise, an error occurs when data is obtained.
- // 2. serialize the object
- Dest. writeInt (id );
- Dest. writeString (name );
- }
- }