Serialization of objects between activity

Source: Internet
Author: User

Activity can jump and pass data through intent and bundles, data types that can be passed include basic data types, A string and an object that implements the Parcelable interface or serializable interface (file implements the Serializable interface):

The 1.Serializable interface is implemented in Java, without overriding any methods, but the serialization and deserialization process produces a large number of intermediate variables that occupy large amounts of memory

2.Parcelable interface, is Google specifically for Android serialization customization, the serialization process needs to rewrite Writetoparcel (Parcel dest, int flag) method, A parcelable.create<t> variable needs to be implemented when deserializing.


To implement the Parcelable interface for serialization and deserialization:

Package Com.hust.andoirdstudy;import Android.graphics.bitmap;import Android.os.parcel;import android.os.Parcelable ;p Ublic class Person implements Parcelable{public string Name;public string birthplace;public int age;public Bitmap icon;p Ublic person () {//Empty construction method} @Overridepublic int describecontents () {return 0;} @Overridepublic void Writetoparcel (Parcel dest, int flags) {//Writetoparcel is responsible for speaking the This object is written to the Parcel file dest.writestring (name );d est.writestring (birthplace);d est.writeint (age); Icon.writetoparcel (dest, 0);} public static final parcelable.creator<person> CREATE = new creator<person> () {// The Create method realizes that the serialized object in Parcel is deserialized into object@overridepublic person Createfromparcel (Parcel source) {Person person = new person ( );p Erson.name = source.readstring ();p erson.birthplace = source.readstring ();p erson.age = Source.readint (); Person.icon = Bitmap.CREATOR.createFromParcel (source); return person;} @Overridepublic person[] NewArray (int size) {return new person[size];};}

To implement the serializable interface for serialization and deserialization:

Package Com.hust.andoirdstudy;import Java.io.serializable;import Android.graphics.bitmap;public class person Implements serializable{/** *  */private static final Long serialversionuid = 1l;public string Name;public string Birth place;public int age;public Bitmap icon;public person () {//Empty construction Method}}

Once you have implemented any of these interfaces, you can pass an object between two activity:

/*** assumes that there is already a person object that stores data */person person = new person ();

Sender-parcelable:

Intent Intent = new Intent (mainactivity.this, Secondactivity.class); Intent.putextra ("PersonID", 1110); Pass the normal data//pass implements the Parcelable interface of the object, of course, ordinary data can also be placed in bundles to pass bundle bundle = new bundle (); Bundle.putparcelable ("Person", person); Intent.putextras (bundle); startactivity (intent);

Receiving party-parcelable:

int personID = Getintent (). Getextras (). GetInt ("PersonID"); Person person = getintent (). Getparcelableextra ("person");


Sender-serializable:

Intent Intent = new Intent (mainactivity.this, Secondactivity.class); Bundle bundle = new bundle (); Bundle.putserializable ("person", person); startactivity (intent);

Receiving party-serializable:

Person P = (person) getintent (). Getserializableextra ("person");

[note]

The essence of serialization is to write the property information of the object in a certain order (in the Parcelable interface, in the order in which it is written in Writetoparcel), and in the deserialization process, read in the previous order, and create an assignment to a new object. Therefore, the order of the object property in the Writetoparcel (Parcel, int) function must be written to and Createfromparcel in the parcelable.creator<t> (Parcel). Read in the same order, otherwise the deserialized object will have the phenomenon of data confusion.

Serialization of objects between activity

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.