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 written
Parcel
And can be recovered from it, and this class must have
static
Of
field
And the name must be
CREATOR
, This
field
Is an implementation
Parcelable.Creator
The 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 );