Using bundles to pass objects in Android via intent

Source: Internet
Author: User

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
  1. /**
  2. * Personser
  3. *
  4. * @author Http://blog.csdn.net/zuolongsnail
  5. */
  6. Public class Personser implements Serializable {
  7. /** 
  8. * 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.
  9. */
  10. private Static final long serialversionuid = -7620435178023928252l;
  11. private String name;
  12. private int age;
  13. Public String GetName () {
  14. return name;
  15. }
  16. public void SetName (String name) {
  17. this.name = name;
  18. }
  19. public int getage () {
  20. return age;
  21. }
  22. public void Setage (int.) {
  23. this.age = age;
  24. }
  25. }

Parcelable Way:

[Java]View Plaincopy
  1. /**
  2. * Personpar
  3. *
  4. * @author Http://blog.csdn.net/zuolongsnail
  5. */
  6. Public class Personpar implements Parcelable {
  7. private String name;
  8. private int age;
  9. Public String GetName () {
  10. return name;
  11. }
  12. public void SetName (String name) {
  13. this.name = name;
  14. }
  15. public int getage () {
  16. return age;
  17. }
  18. public void Setage (int.) {
  19. this.age = age;
  20. }
  21. /** 
  22. * Serialization of entity classes
  23. */
  24. public static final parcelable.creator<personpar> Creator = new creator<personpar> () {
  25. Public Personpar Createfromparcel (Parcel source) {
  26. Personpar Personpar = new Personpar ();
  27. Personpar.name = Source.readstring ();
  28. Personpar.age = Source.readint ();
  29. return personpar;
  30. }
  31. Public personpar[] NewArray (int size) {
  32. return new personpar[size];
  33. }
  34. };
  35. @Override
  36. public int describecontents () {
  37. return 0;
  38. }
  39. /** 
  40. * Write entity class data to parcel
  41. */
  42. @Override
  43. public void Writetoparcel (Parcel Parcel, int flags) {
  44. Parcel.writestring (name);
  45. Parcel.writeint (age);
  46. }
  47. }
The second step: passing the serialized object;

[Java]View Plaincopy
  1. Intent Intent = new Intent (mainactivity. This, destactivity.  class);
  2. Bundle bundle = new bundle ();
  3. Switch (V.getid ()) {
  4. Case R.ID.SERIALIZABLE_BTN:
  5. //Serializable passing objects
  6. Personser Personser = new Personser ();
  7. Personser.setname ("Zuolong");
  8. Personser.setage (26);
  9. Bundle.putserializable (Ser_key, Personser);
  10. Intent.putextra ("type", Ser_type);
  11. Intent.putextras (bundle);
  12. StartActivity (Intent);
  13. Break ;
  14. Case R.ID.PARCELABLE_BTN:
  15. //Parcelable passing Objects
  16. Personpar Personpar = new Personpar ();
  17. Personpar.setname ("snail");
  18. Personpar.setage (27);
  19. //Bundle has Putparcelablearray and Putparcelablearraylist methods, you can also pass arrays and lists
  20. Bundle.putparcelable (Par_key, Personpar);
  21. Intent.putextra ("type", Par_type);
  22. ///Intent also has a Putparcelablearraylistextra method that can pass a list of objects that implement the Parcelable interface
  23. Intent.putextras (bundle);
  24. StartActivity (Intent);
  25. Break ;
  26. }
Step Three: Get the object;

[Java]View Plaincopy
  1. StringBuffer sb = new StringBuffer ();
  2. String type = Getintent (). Getstringextra ("type");
  3. Determine which type is based on type
  4. if (Type.equals (Mainactivity.ser_type)) {
  5. //Get Serializable object
  6. Personser Personser = (personser) getintent (). Getserializableextra (
  7. Mainactivity.ser_key);
  8. Sb.append ("----from Serializable----"). Append ("\ n");
  9. Sb.append ("Name:"). Append (Personser.getname ()). Append ("\ n");
  10. Sb.append ("Age:"). Append (Personser.getage ()). Append ("\ n");
  11. } Else if (type.equals (Mainactivity.par_type)) {
  12. //Get Parcelable object
  13. Personpar Personpar = (personpar) getintent (). Getparcelableextra (
  14. Mainactivity.par_key);
  15. Sb.append ("----from parcelable----"). Append ("\ n");
  16. Sb.append ("Name:"). Append (Personpar.getname ()). Append ("\ n");
  17. Sb.append ("Age:"). Append (Personpar.getage ()). Append ("\ n");
  18. }

Attached Source:

Operation Result:


Using bundles to pass objects in Android via intent

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.