Android uses bundle to pass objects through intent
In Android development, you sometimes need to pass objects between applications or between processes, and here's a detailed description of how intent uses bundle to pass objects.
The object being passed needs to be serialized first, and there are two ways to serialize the object:java.io.Serializable and Android.os.Parcelable
Serializable is used in Java, and Google uses a custom parcelable on Android.
The difference between the two serialization modes:
1. In the use of memory, parcelable than serializable performance, recommended the use of parcelable class;
2.Serializable produces a large number of temporary variables when serializing, resulting in frequent GC;
3.Parcelable cannot be used in situations where you want to store data on disk because Parcelable does not guarantee the continuity of the data in the event of a change in the environment,
This situation suggests the use of serializable.
The first step: Define the serialized entity class;
Serializable way:
/**
* Personser * *
@author http://blog.csdn.net/zuolongsnail/public
class Personser Implements Serializable {
/**
* serialversionuid role is to modify the entity class, can be normal serialization and deserialization, in this not to say, we can Google Baidu under.
*
private static final long serialversionuid = -7620435178023928252l;
private String name;
private int age;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
}
Parcelable Way:
/** * Personpar * * @author http://blog.csdn.net/zuolongsnail/public class Personpar implements Parcelable {
private String name;
private int age;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
public int getage () {return age;
public void Setage (int age) {this.age = age; /** * Serialization Entity class * * public static final parcelable.creator<personpar> Creator = new CREATOR<PERSONPAR&G
t; () {Public Personpar createfromparcel (Parcel source) {Personpar Personpar = new Personpar ();
Personpar.name = Source.readstring ();
Personpar.age = Source.readint ();
return personpar;
Personpar[] NewArray (int size) {return new personpar[size];
}
};
@Override public int describecontents () {return 0; /** * Writes entity class data to Parcel */@Override public void Writetoparcel (Parcel Parcel, int flags) {PARCEL.WRITESTR
ing (name); Parcel.writeint (age);
}
}
Step Two: Pass the serialized object;
Intent Intent = new Intent (mainactivity.this, destactivity.class);
Bundle Bundle = new Bundle ();
Switch (V.getid ()) {case
r.id.serializable_btn:
//Serializable Delivery object
personser Personser = new Personser () ;
Personser.setname ("Zuolong");
Personser.setage ();
Bundle.putserializable (Ser_key, personser);
Intent.putextra ("type", Ser_type);
Intent.putextras (bundle);
StartActivity (intent);
break;
Case R.ID.PARCELABLE_BTN:
//Parcelable Transfer object
personpar personpar = new Personpar ();
Personpar.setname ("snail");
Personpar.setage (a);
Bundle has Putparcelablearray and Putparcelablearraylist methods, and can also pass arrays and lists
bundle.putparcelable (Par_key, personpar);
Intent.putextra ("type", Par_type);
Intent also has the Putparcelablearraylistextra method, which can pass the list of objects that implements the Parcelable interface
Intent.putextras (bundle);
StartActivity (intent);
break;
The third step: Obtain the object;
StringBuffer sb = new StringBuffer ();
String type = Getintent (). Getstringextra ("type");
According to type, what kind of
if (Type.equals (Mainactivity.ser_type)) {
//Get serializable object
personser Personser = ( Personser) getintent (). Getserializableextra (
mainactivity.ser_key);
Sb.append ("----from Serializable----"). Append ("\ n");
Sb.append ("Name:"). Append (Personser.getname ()). Append ("\ n");
Sb.append ("Age:"). Append (Personser.getage ()). Append ("\ n");
else if (type.equals (Mainactivity.par_type)) {
//Get Parcelable object
personpar personpar = (personpar) getintent (). Getparcelableextra (
mainactivity.par_key);
Sb.append ("----from parcelable----"). Append ("\ n");
Sb.append ("Name:"). Append (Personpar.getname ()). Append ("\ n");
Sb.append ("Age:"). Append (Personpar.getage ()). Append ("\ n");
Attached Source: Download the Address
Run Results screenshot:
Thank you for reading, I hope to help you, thank you for your support for this site!