Ways to pass data using intent in Android development

Source: Internet
Author: User

Activity passes values between intent, supports basic data types and string objects and their array objects byte, byte[], Char, char[], Boolean, boolean[], short, short[], int, int [], Long, long[], float, float[], double, double[], String, string[], and adopt the implementation of Serializable, The Parcelable interface's class object passes data in two ways: one is bundle.putserializable (Key,object) and the other is Bundle.putparcelable (Key, Object)

For example:

[Java]View Plaincopyprint?
  1. Import Android.os.Parcel;
  2. Import android.os.Parcelable;
  3. Public class book implements Parcelable {
  4. private String BookName;
  5. private String author;
  6. private int publishtime;
  7. Public String Getbookname () {
  8. return bookname;
  9. }
  10. public void Setbookname (String bookname) {
  11. this.bookname = bookname;
  12. }
  13. Public String Getauthor () {
  14. return author;
  15. }
  16. public void Setauthor (String author) {
  17. This.author = author;
  18. }
  19. public int Getpublishtime () {
  20. return publishtime;
  21. }
  22. public void setpublishtime (int publishtime) {
  23. this.publishtime = publishtime;
  24. }
  25. public static final parcelable.creator<book> Creator = new creator<book> () {
  26. Public book Createfromparcel (Parcel source) {
  27. Book Mbook = new book ();
  28. Mbook.bookname = Source.readstring ();
  29. Mbook.author = Source.readstring ();
  30. Mbook.publishtime = Source.readint ();
  31. return mbook;
  32. }
  33. Public book[] NewArray (int size) {
  34. return new book[size];
  35. }
  36. };
  37. public int describecontents () {
  38. return 0;
  39. }
  40. public void Writetoparcel (Parcel Parcel, int flags) {
  41. Parcel.writestring (BookName);
  42. Parcel.writestring (author);
  43. Parcel.writeint (Publishtime);
  44. }
  45. }

Implement the Serializable interface:

[Java]View Plaincopyprint?
  1. Import java.io.Serializable;
  2. Public class Person implements Serializable {
  3. private Static final long serialversionuid = -7060210544600464481l;
  4. private String name;
  5. private int age;
  6. Public String GetName () {
  7. return name;
  8. }
  9. public void SetName (String name) {
  10. this.name = name;
  11. }
  12. public int getage () {
  13. return age;
  14. }
  15. public void Setage (int.) {
  16. this.age = age;
  17. }
  18. }

This allows you to write the pass-through data code in the activity:

[Java]View Plaincopyprint?
    1. Intent mintent = new Intent (this, address activity.     Class);
    2. Bundle Mbundle = new Bundle ();
    3. Transfer data using parcelable
    4. Mbundle.putparcelable ("Data", Mbook);
    5. Transfer data using Serializable
    6. Mbundle.putserializable ("data", items);
    7. Mintent.putextras (Mbundle);
    8. StartActivity (mintent);

Accepted data can be used:

[Java]View Plaincopyprint?
    1. Intent Intent = Getintent ();
    2. arraylist<map<string, string>> items = (arraylist<map<string, string>>) Intent.getExtras ().  Get ("data");

Ways to pass data using intent in Android development

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.