The role of "analysis" parcelable

Source: Internet
Author: User

First, Introduction

1.Parcelable is an interface that enables serialization.

2. The role of serialization is reflected in: You can use intent to pass data, or you can build a process to pass data (IPC).

3.Parcelable when used, there is a parameter type: Parcel; it is like a container, when serialized, the data is passed in. When deserializing, the data is then fetched.

4.Parcelable is no longer going to be able to deposit data to disk. Because, parcelable can not effectively guarantee the continuity of the data under the circumstance of the outside.

Ii. Examples of Use

1. The definition of the data entity and parcelable being manipulated.

1  Packagecom.app.suodr.serialization;2 3 ImportAndroid.os.Parcel;4 Importandroid.os.Parcelable;5 6 /**7 * Wangpei on 2015/7/16 09:438 * Email: [Email protected]9  *Ten * Map your objects into parcel objects by writetoparcel, and then map parcel objects to your objects through Createfromparcel.  One * You can also think of parcel as a stream through which objects are written into the stream, and the object is read from the stream through the Createfromparcel, A * Only this process requires you to implement, so the order of writing and the order of reading must be consistent.  -  */ -  Public classParcelable_personImplementsparcelable{ the  -      PublicString name; -      Public intAge ; -  +      PublicParcelable_person (String name,intAge ) { -          This. Name =name; +          This. Age =Age ; A     } at  -      PublicString GetName () { -         returnname; -     } -  -      Public voidsetName (String name) { in          This. Name =name; -     } to  +      Public intGetage () { -         returnAge ; the     } *  $      Public voidSetage (intAge ) {Panax Notoginseng          This. Age =Age ; -     } the  + @Override A      Public intdescribecontents () { the         return0; +     } -  $ @Override $      Public voidWritetoparcel (Parcel dest,intflags) { - Dest.writeint (age); - dest.writestring (name); the     } - Wuyi      Public Static FinalCreator<parcelable_person> Creator =NewCreator<parcelable_person>() { the @Override -          PublicParcelable_person Createfromparcel (Parcel source) { Wu             return NewParcelable_person (source); -         } About  $ @Override -          PublicParcelable_person[] NewArray (intsize) { -             return NewParcelable_person[size]; -         } A     }; +  the      PublicParcelable_person (Parcel in) { -Age =in.readint (); $Name =in.readstring (); the     } the}

2. Use intent to transfer data into: The delivery party and the receiver

Delivery Party definition:

1  Packagecom.app.suodr.serialization;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 7 Importcom.app.suodr.ik_family. R;8 9 /**Ten * Wangpei on 2015/7/16 10:06 One * Email: [Email protected] A  */ -  Public classParcelable_postextendsactivity{ -  the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); +Intent i =getintent (); -  +Parcelable_person person =NewParcelable_person ("Wangpei", 100); A  atI.putextra ("Yes", person); -I.setclass ( This, Parcelable_get.class); - startactivity (i); -          This. Finish (); -     } -}

Receiver definition:

1  Packagecom.app.suodr.serialization;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.util.Log;7 8 Importcom.app.suodr.ik_family. R;9 Ten /** One * Wangpei on 2015/7/16 09:29 A * Email: [Email protected] -  */ -  Public classParcelable_getextendsactivity{ the  -     Private Static FinalString TAG = "MyInfo"; -  - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.main); A  atIntent i =getintent (); -Parcelable_person person = I.getparcelableextra ("Yes"); -  -LOG.I (TAG, "Name:" +person.name+ "Age:" +person.age); -     } -}

The role of "analysis" parcelable

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.