"Go" the use of intent transitive objects and bundles in Android

Source: Internet
Author: User

Original URL: http://blog.csdn.net/lixiang0522/article/details/8642202

The objects passed between components in Android generally implement the Parcelable interface, of course, you can also use the Java serializable interface, the former is specifically designed for Android, more efficient, below we will implement a parcelabel. 1. Create a class to implement the Parcelable interface, which is implemented as follows: [Java]View PlainCopy
  1. Package com.hebaijun.testparcelable;
  2. Import Android.os.Parcel;
  3. Import android.os.Parcelable;
  4. Public class Parcelabledata implements parcelable{
  5. private String name;
  6. private int age;
  7. Public Parcelabledata () {
  8. Name = "Guest";
  9. Age = 20;
  10. }
  11. Public Parcelabledata (Parcel in) {
  12. //order in the same order as the Writetoparcel write
  13. Name = In.readstring ();
  14. Age = In.readint ();
  15. }
  16. Public String GetName () {
  17. return name;
  18. }
  19. public void SetName (String name) {
  20. this.name = name;
  21. }
  22. public int getage () {
  23. return age;
  24. }
  25. public void Setage (int.) {
  26. this.age = age;
  27. }
  28. @Override
  29. public int describecontents () {
  30. //TODO auto-generated method stub
  31. return 0;
  32. }
  33. @Override
  34. public void Writetoparcel (Parcel dest, int flags) {
  35. //TODO auto-generated method stub
  36. Dest.writestring (name);
  37. Dest.writeint (age);
  38. }
  39. public static final parcelable.creator<parcelabledata> Creator = new parcelable.creator< Parcelabledata> () {
  40. Public Parcelabledata Createfromparcel (Parcel in) {
  41. return new Parcelabledata (in);
  42. }
  43. Public parcelabledata[] NewArray (int size) {
  44. return new parcelabledata[size];
  45. }
  46. };
  47. }


2. Send the object by the following method. The bundle class also implements the Parcelable interface, typically in Android we encapsulate the data and transmit it through bundles. [Java]View PlainCopy
  1. Intent Intent = new Intent ();
  2. Intent.setclass (This, subactivity.   Class);
  3. Add directly
  4. Intent.putextra ("MyData", New Parcelabledata ());
  5. Through bundles
  6. Bundle bundle = new bundle ();
  7. Bundle.putstring ("MyString", "Test bundle");
  8. Bundle.putparcelable ("MyData", new Parcelabledata ());
  9. Intent.putextras (bundle);
  10. StartActivity (Intent);


3. The following method of receiving the object. [Java]View PlainCopy
    1. Parcelabledata parcelabledata = Getintent (). Getparcelableextra ("MyData");
    2. Bundle bundle = Getintent (). Getextras ();
    3. Parcelabledata parcelabledata = bundle.getparcelable ("MyData");
    4. String testbundlestring = bundle.getstring ("MyString");
    5. LOG.V ("string=", testbundlestring);
    6. LOG.V ("Name=", Parcelabledata.getname ());
    7. LOG.V ("age=", " " +parcelabledata.getage ());

The transferred object needs to be serialized: There are two ways to implement the Serializable interface, which is the original Java mode, and the other is the parcelable mode of Android, this performance may be better, I guess, But this is required to manually write the implementation of the Parcelable interface.

Serializable Storage data:

  1. Person Mperson = new Person ();
  2. Mperson.setname ("Frankie");
  3. Mperson.setage (25);
  4. Intent mintent = new Intent (this,objecttrandemo1.   Class);
  5. Bundle Mbundle = new Bundle ();
  6. Mbundle.putserializable (Ser_key,mperson);
  7. Mintent.putextras (Mbundle);

Serializable fetch data:

 // 获取启动该ResultActivity的Intent
24         Intent intent = getIntent();
25         // 获取该Intent所携带的数据
26         Bundle bundle = intent.getExtras();
27         // 从bundle数据包中取出数据
28         Person person = (Person) bundle.getSerializable("person");

Parcelable Storage Data:

    1. Intent mintent = new Intent (This,objecttrandemo2.   Class);
    2. Bundle Mbundle = new Bundle ();
    3. Mbundle.putparcelable (Par_key, Mbook);
    4. Mintent.putextras (Mbundle);

parcelable Fetch data:


    1. Book Mbook = (book) getintent (). Getparcelableextra (Objecttrandemo.par_key);

Reference 1:http://blog.csdn.net/android_tutor/article/details/5740845

Reference 2:http://my.oschina.net/u/577632/blog/76906

"Go" the use of intent transitive objects and bundles in Android

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.