Passing objects between Android activities

Source: Internet
Author: User

There are two ways to pass objects between different Android activities:

Bundle 1) Public void putserializable (string key, serializable value); 2) Public void putparcelable (string key, parcelable value)

Therefore, the transfer class must be implements serializable or implements parcelable.

1) implements serializable and parcelable:

1) serializable: mobile. Java

package com.example.objecttest.myclass;import java.io.Serializable;public class Mobile implements Serializable {private static final long serialVersionUID = 5403208217014231126L;private String model;public Mobile(String model) {super();this.model = model;}public String getModel() {return model;}public void setModel(String model) {this.model = model;}}

 

2) public interface parcelable

If a class implements this interface, its object instance can be writtenParcelAnd can be recovered from it, and this class must havestaticOffieldAnd the name must beCREATOR, ThisfieldIs an implementationParcelable.CreatorThe object instance of the interface class.

Public static interface parcelable. creator <t>

Interface that must be implemented and provided as a public creator field that generates instances of your parcelable class from a parcel

It has two member functions: createfromparcel and newarray.

The createfromparcel function creates a new object instance for a class that implements the parcelable interface, and instantiates the data obtained from the given parcel. The data in parcel is previously passed through parcelable. data written in writetoparcel mode.

The newarray function creates a new object array for a class that implements the parcelable interface. Each item in the array is initialized to null.

 

Parcelable: pad, Java

package com.example.objecttest.myclass;import android.os.Parcel;import android.os.Parcelable;public class Pad implements Parcelable {private String model;public Pad(String model) {super();this.model = model;}public static final Parcelable.Creator<Pad> CREATOR = new Creator<Pad>() {public Pad createFromParcel(Parcel source) {Pad mBook = new Pad(source.readString());return mBook;}public Pad[] newArray(int size) {return new Pad[size];}};@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {// TODO Auto-generated method stubdest.writeString(model);}//public String getModel() {return model;}public void setModel(String model) {this.model = model;}}

 

2) usage:

1)

/**
* Serializeable Method for passing objects
*/
Private void serializemethod (){
Mobile mmobile = new mobile ("me811 ");
Intent mintent = new intent (this, serializableactivity. Class );
Bundle mbundle = new bundle ();
Mbundle. putserializable (ser_key, mmobile );
Mintent. putextras (mbundle );

Startactivity (mintent );
}

 

2)

/**
* Object transfer method of pacelable
*/
Private void pacelablemethod (){
Pad mpolice = new pad ("Nexus 7 ");
Intent mintent = new intent (this, parcelableactivity. Class );
Bundle mbundle = new bundle ();
Mbundle. putparcelable (par_key, mpolice );
Mintent. putextras (mbundle );

Startactivity (mintent );
}

 

3) obtain the passed results:

1) mobile mperson = (mobile) getintent (). getserializableextra (ser_key );

2) pad mpad = (PAD) getintent (). getparcelableextra (par_key );

 

 

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.