Android Intent passing objects in 3 ways to explain _android

Source: Internet
Author: User

Objective

Believe that the use of intent is familiar, intent can be used to start activity,service and so on, and we can also pass the data through the intent, when we use intent to transfer information between the activity to find putExtra() method does not support the transfer of custom objects, here are three ways to solve them.

First, Serializable Way

This is the simplest method, because all we have to do is let our custom object implement Serializable this empty interface.

public class person implements serializable{
    private String mname;
    Private String maddress;

    Public String GetName () {return
      mname;
    }

    public void SetName (String name) {
        mname = name;
    }

    Public String getaddress () {return
        maddress;
    }

    public void setaddress (String address) {
        maddress = address;
    }
}

At this point, you can already use the intent putExtra() method to pass this custom object, which is used in the activity

      person who = new person ();
      Person.setname ("Hwaphon");
      Person.setaddress ("Anhui");

      Intent Intent = new Intent (mainactivity.this,secondactivity.class);
      Intent.putextra ("person", person);
      StartActivity (intent);
      Finish ();

Receiving data in the activity

 Intent Intent = Getintent ();
person who = (person) intent.getserializableextra (' person ');

Using the Serializable method is very simple, but the efficiency is not optimistic, because it will serialize the entire object, the cost is very large, in order to pursue efficiency, we have to use another method, that is, the Parcelable method.

Second, Parcelable Way

Enables the custom class to implement the Parcelable interface, which must override two methods at this time

1. describeContents() : Used to describe the content interface, generally direct return 0 can be

2. writeToParcel() : Used to write the data you want to pass into the parcel container.

In addition to these two methods, we also need to create an implementation of the Parcelable.Creator interface, this interface also requires us to implement two methods

1. createFromParcel() : Used to read the data written in the parcel container, instantiate an object with the read data, and return it.

2. newArray() : Create an array of size and return, generally, directly return T[size] .

public class person implements parcelable{private String mname;

    Private String maddress;
    Public String GetName () {return mname;
    public void SetName (String name) {mname = name;
    Public String getaddress () {return maddress;
    public void setaddress (String address) {maddress = address;
    @Override public int describecontents () {return 0;
      @Override public void Writetoparcel (Parcel Parcel, int i) {parcel.writestring (mname);
    Parcel.writestring (maddress); public static final parcelable.creator<person> Creator = new parcelable.creator<person> () {@Overrid
          E createfromparcel (Parcel Parcel) {Person person = new person ();
          Person.mname = Parcel.readstring ();
          Person.maddess = Parcel.readstring ();
      return person; @Override public person[] NewArray (int i) {return New Person[i];
}
    }; }

This time the reception method is somewhat different from the serializable, as follows

 Intent Intent = Getintent ();
 person who = (person) intent.getparcelableextra (' person ');

In the writeToParcel() method, we can write the data we want to the container, if it is the data we do not need, then we can discard the write, which is the reason why the Parcelabel method is more efficient than the Serializable method. However, this method is much more than the serializable method to implement the code, is there any way to change this situation? You can use a key to generate the code, such as the Android Parcelable code generator plug-in, you can download the plugin directly in the Android Studio plugins, after downloading, click ALT + Insert to see the parcelable option.

Three, JSON Way

This time the custom object class is

public class person{
    private String mname;
    Private String madress;

    Public String GetName () {return
      mname;
    }

    public void SetName (String name) {
      mname = name;
    }

    Public String getadress () {return
        madress;
    }

    public void setadress (String adress) {
      madress = adress;
    }
  }

How to use

 Send
 Intent Intent = new Intent (mainactivity.this,secondactivity.class);

 Intent.putextra (' person ', new Gson (). Tojson (person));


Receive
 Intent Intent = Getintent ();
 String jsondata = Intent.getstringextra ("person");
 Person person = new Gson (). Fromjson (Jsondata,person.class);

Do not forget to import the Gson package when using, otherwise it is not available.

Summarize

This is the full content of this article, I hope you can help Android developers, if you have questions you can leave a message exchange.

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.