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?
- Import Android.os.Parcel;
- Import android.os.Parcelable;
- Public class book implements Parcelable {
- private String BookName;
- private String author;
- private int publishtime;
- 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 Getpublishtime () {
- return publishtime;
- }
- public void setpublishtime (int publishtime) {
- this.publishtime = publishtime;
- }
- public static final parcelable.creator<book> Creator = new creator<book> () {
- Public book Createfromparcel (Parcel source) {
- Book Mbook = new book ();
- Mbook.bookname = Source.readstring ();
- Mbook.author = Source.readstring ();
- Mbook.publishtime = Source.readint ();
- return mbook;
- }
- Public book[] NewArray (int size) {
- return new book[size];
- }
- };
- public int describecontents () {
- return 0;
- }
- public void Writetoparcel (Parcel Parcel, int flags) {
- Parcel.writestring (BookName);
- Parcel.writestring (author);
- Parcel.writeint (Publishtime);
- }
- }
Implement the Serializable interface:
[Java]View Plaincopyprint?
- Import java.io.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.) {
- this.age = age;
- }
- }
This allows you to write the pass-through data code in the activity:
[Java]View Plaincopyprint?
- Intent mintent = new Intent (this, address activity. Class);
- Bundle Mbundle = new Bundle ();
- Transfer data using parcelable
- Mbundle.putparcelable ("Data", Mbook);
- Transfer data using Serializable
- Mbundle.putserializable ("data", items);
- Mintent.putextras (Mbundle);
- StartActivity (mintent);
Accepted data can be used:
[Java]View Plaincopyprint?
- Intent Intent = Getintent ();
- arraylist<map<string, string>> items = (arraylist<map<string, string>>) Intent.getExtras (). Get ("data");
Ways to pass data using intent in Android development