Objects are transmitted between two androidactivities.

Source: Internet
Author: User

Objects are transmitted between two androidactivities.

To transfer data between android activities, the Intent object method is mainly used.

The transfer of basic data types is quite simple, mainly through the following methods:

PutExtra method.

This is not described here.


It mainly transmits object type data.

1. The Object class must first serialize and implement the Serializable interface, and use Bundle. putSerializable (Key, Object) to complete data transmission.

2. The Object class implements the Parcelable interface and uses Bundle. putParcelable (Key, Object) to complete data transmission.


In another activity, you can use getIntent (). getSerializableExtra ("key") or getIntent (). getParcelableExtra ("key") to obtain data.

Steps for implementing Parcelable

1) implements Parcelable

2) rewrite the writeToParcel method to serialize your object into a Parcel object, that is, write the data of the class to the external provided Parcel, and package the data to be transferred to the Parcel container for storage, to get data from the Parcel container

3) override the describeContents method and content interface description. By default, 0 is returned.

4) instantiate static internal Object CREATOR implementation interface Parcelable. Creator

public static final Parcelable.Creator
 
   CREATOR
 

Note:Public static final cannot be less than one, and the name of the internal Object CREATOR cannot be changed. It must be capitalized. You need to override the two methods in this interface: createFromParcel (Parcel in) to read and pass data values from the Parcel container, encapsulate them into Parcelable objects and return to the logic layer, newArray (int size) create an array of T type and size. return new T [size] in only one sentence for external class deserialization.

In short:Use writeToParcel to map your object to a Parcel object, and then use createFromParcel to map the Parcel object to your object. You can also regard Parcel as a stream, write the object to the stream through writeToParcel, and read the object from the stream through createFromParcel, but you need to implement this process, therefore, the write sequence must be consistent with the read sequence.

The Code is as follows:

public class MyParcelable implements Parcelable {     private int mData;     public int describeContents()      {         return 0;     }     public void writeToParcel(Parcel out, int flags)      {         out.writeInt(mData);     }     public static final Parcelable.Creator
 
   CREATOR = new Parcelable.Creator
  
   ()      {         public MyParcelable createFromParcel(Parcel in)          {             return new MyParcelable(in);         }         public MyParcelable[] newArray(int size)          {             return new MyParcelable[size];         }     };          private MyParcelable(Parcel in)      {         mData = in.readInt();     } }
  
 

Differences between Serializable implementation and Parcelabel implementation

1) The implementation of Serializable only requires implements Serializable. This only marks the object and the system will automatically serialize it.

2) The implementation of Parcelabel requires not only implements Parcelabel, but also adding a static member variable CREATOR to the class. This variable must implement the Parcelable. Creator interface.

Code comparison:

1) create a Person class to implement Serializable

public class Person implements Serializable{    private static final long serialVersionUID = -7060210544600464481L;    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;    }}

2) create a Book class to implement Parcelable

public class Book implements Parcelable{    private String bookName;    private String author;    private int publishDate;        public Book()    {            }        public String getBookName()    {        return bookName;    }        public void setBookName(String bookName)    {        this.bookName = bookName;    }        public String getAuthor()    {        return author;    }        public void setAuthor(String author)    {        this.author = author;    }        public int getPublishDate()    {        return publishDate;    }        public void setPublishDate(int publishDate)    {        this.publishDate = publishDate;    }        @Override    public int describeContents()    {        return 0;    }        @Override    public void writeToParcel(Parcel out, int flags)    {        out.writeString(bookName);        out.writeString(author);        out.writeInt(publishDate);    }        public static final Parcelable.Creator
 
   CREATOR = new Creator
  
   ()    {        @Override        public Book[] newArray(int size)        {            return new Book[size];        }                @Override        public Book createFromParcel(Parcel in)        {            return new Book(in);        }    };        public Book(Parcel in)    {        bookName = in.readString();        author = in.readString();        publishDate = in.readInt();    }}
  
 

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.