Chapter 5 data-centric-Data Access (3), Chapter 5 Access

Source: Internet
Author: User
Tags java se

Chapter 5 data-centric-Data Access (3), Chapter 5 Access
5.1.3 free operation-serialization and deserialization

In Android development, apart from common text and XML files, data is often transmitted or accessed through serialization and deserialization.

Android serialization objects can be implemented in two ways: Serializable interface or Parcelable interface. The Serializable interface is supported by Java SE, and Parcelable is a feature specific to Android, which is more efficient than the Serializable interface and can also be used in 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 using this method to improve performance.

The following describes how to use the Parcelable interface to transfer objects between two activities. bundle. putParcelable is used to transfer objects.

1) Declare the implementation interface Parcelable.

// Import omitted

Public class Person implements Parcelable {

 

Protected String name;

Protected String age;

 

Person (String name, String age ){

This. name = name;

This. age = age;

}

Person (Parcel in ){

Name = in. readString ();

Age = in. readString ();

}

Public String getName (){

Return name;

}

Public void setName (String name ){

This. name = name;

}

Public String getAge (){

Return age;

}

Public void setAge (String age ){

This. age = age;

}

@ Override

Public int describeContents (){

Return 0;

}

/**

* The Parcelable method writeToParcel serializes your object into a Parcel object.

*/

@ Override

Public void writeToParcel (Parcel dest, int flags ){

Dest. writeString (name );

Dest. writeString (age );

}

/**

* Instantiate the static internal Object CREATOR and implement the interface Parcelable. Creator

*/

Public static final Creator <Person> CREATOR = new Creator <Person> (){

Public Person createFromParcel (Parcel in ){

Return new Person (in );

}

Public Person [] newArray (int size ){

Return new Person [size];

}

};}

 

2) Implement Parcel Object serialization for your object and put Parcelable into Bundle.

// Import omitted

Public class ParcelableActivity1 extends Activity {

 

Private Button myButton;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

MyButton = new Button (this );

MyButton. setOnClickListener (new OnClickListener (){

@ Override

Public void onClick (View arg0 ){

// Create a Person object

Person benParcelable = new Person ("testname", "testage ");

Intent intent = new Intent ();

Intent. setClass (getApplicationContext (),

ParcelableActivity2.class );

Bundle bundle = new Bundle ();

// Put the serialized object into bundle

Bundle. putParcelable ("person", benParcelable );

Intent. putExtras (bundle );

// Start Activity of ParcelableActivity2

StartActivity (intent );

}

});

SetContentView (myButton );

}

}

 

3) the implementation method createFromParcel deserializes the Parcel object into your object.

// Import omitted

Public class ParcelableActivity2 extends Activity {

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

// Obtain the deserialized Person object

Person parcelable = getIntent (). getParcelableExtra ("person ");

// Print the output

System. out. println (parcelable. getName ());

System. out. println (parcelable. getAge ());

}

}

---------------------------------------- It is difficult for programmers to make money. Therefore, they must learn financial management. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. We recommend investing in don't invest in security, which is almost impossible to transfer. It is very difficult to withdraw the website link in advance. Don't make it in white --------------------------------------------

4) The result is displayed, as shown in Figure 5-2.

Figure 5-2 serialized deserialization results

 

Experience Sharing:

In addition to the ability to transmit Object-type data between activities through serialization and deserialization, we can also use it to save and read data.

Compared with plain text files and XML files, serialized files can be binary files rather than plain text files and cannot be directly read. Therefore, if the stored data has certain security requirements, and the security level is not very high, you can consider storing the data in serialization mode and then reading the data in deserialization mode.

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.