Intentbundle transitive objects serializableparcelableandroid sometimes need to pass objects in the app or between processes, the following details intent methods for passing objects using bundles.
The passed object needs to be serialized first, and there are two ways of serializing the object: Java.io.Serializable and Android.os.Parcelable
Java uses serializable, and Google uses a custom parcelable on Android.
The difference between the two serialization methods:
1. When using memory, parcelable than serializable performance, recommend the use of parcelable class;
2.Serializable generates a large number of temporary variables at the time of serialization, resulting in frequent GC;
3.Parcelable can not be used in the case of storing data on disk, because Parcelable does not guarantee the continuity of the data in the case of changes in the outside world, this situation is recommended to use serializable.
The first step: Define the serialized entity class;
Serializable way:
[Java]View Plaincopy
- /**
- * Personser
- *
- * @author Http://blog.csdn.net/zuolongsnail
- */
- Public class Personser implements Serializable {
- /**
- * The role of SERIALVERSIONUID 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.) {
- this.age = age;
- }
- }
Parcelable Way:
[Java]View Plaincopy
- /**
- * 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.) {
- this.age = age;
- }
- /**
- * Serialization of entity classes
- */
- public static final parcelable.creator<personpar> Creator = new creator<personpar> () {
- Public Personpar Createfromparcel (Parcel source) {
- Personpar Personpar = new Personpar ();
- Personpar.name = Source.readstring ();
- Personpar.age = Source.readint ();
- return personpar;
- }
- Public personpar[] NewArray (int size) {
- return new personpar[size];
- }
- };
- @Override
- public int describecontents () {
- return 0;
- }
- /**
- * Write entity class data to parcel
- */
- @Override
- public void Writetoparcel (Parcel Parcel, int flags) {
- Parcel.writestring (name);
- Parcel.writeint (age);
- }
- }
The second step: passing the serialized object;
[Java]View Plaincopy
- Intent Intent = new Intent (mainactivity. This, destactivity. class);
- Bundle bundle = new bundle ();
- Switch (V.getid ()) {
- Case R.ID.SERIALIZABLE_BTN:
- //Serializable passing objects
- Personser Personser = new Personser ();
- Personser.setname ("Zuolong");
- Personser.setage (26);
- Bundle.putserializable (Ser_key, Personser);
- Intent.putextra ("type", Ser_type);
- Intent.putextras (bundle);
- StartActivity (Intent);
- Break ;
- Case R.ID.PARCELABLE_BTN:
- //Parcelable passing Objects
- Personpar Personpar = new Personpar ();
- Personpar.setname ("snail");
- Personpar.setage (27);
- //Bundle has Putparcelablearray and Putparcelablearraylist methods, you can also pass arrays and lists
- Bundle.putparcelable (Par_key, Personpar);
- Intent.putextra ("type", Par_type);
- ///Intent also has a Putparcelablearraylistextra method that can pass a list of objects that implement the Parcelable interface
- Intent.putextras (bundle);
- StartActivity (Intent);
- Break ;
- }
Step Three: Get the object;
[Java]View Plaincopy
- StringBuffer sb = new StringBuffer ();
- String type = Getintent (). Getstringextra ("type");
- Determine which type is based on type
- 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:
Operation Result:
Using bundles to pass objects in Android via intent