Android activity passing entity class object

Source: Internet
Author: User

To serialize an object by implementing the Parcelable interface:

1, realize parcelable interface.
2, and implement the public Parcelable interface
void Writetoparcel (Parcel dest, int flags) method.
3. A custom type must contain a static member named Creator, which requires the implementation of the Parcelable.creator interface and its methods.

publicclassPerson implementsParcelable {    publicstaticfinal Parcelable.Creator<Person> CREATOR = newCreator<Person>() {        @Override        publicPerson[] newArray(intsize) {            returnnull;        }        @Override        publicPerson createFromParcel(Parcel source) {            Person result = newPerson();            result.age = source.readInt();            result.name = source.readString();            result.address = source.readParcelable(Address.class.getClassLoader());            returnresult;        }    };    privateAddress address;    privateString name;    privateintage;     @Override    publicintdescribeContents() {        return0;    }    public Address getAddress() {        returnaddress;    }    publicvoidsetAddress(Address address) {        this.address = address;    }    @Override    publicvoidwriteToParcel(Parcel dest, intflags) {        dest.writeInt(age);        dest.writeString(name);        dest.writeParcelable(address, PARCELABLE_WRITE_RETURN_VALUE);    }    publicString getName() {        returnname;    }    publicvoidsetName(String name) {        this.name = name;    }    publicintgetAge() {        return age;    }    publicvoidsetAge(intage) {        this.age = age;    }    @Override    publicString toString() {        return"Person [address="+ address + ", name="+ name + ", age="+ age                + "]";    }}

Passing entity classes:

Bundle bundle = new bundle ();
Bundle.putserializable (title, title);
Bundle.putparcelable (title, title);
Context.startactivity (New Intent (context, target). Putextras (bundle));

Receive:

Mcollectparamobj = (collectparamobj) getintent (). Getparcelableextra (
"title");

Android activity passing entity class object

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.