Android programming uses Intent to pass object method analysis

  • 2020-12-22 17:46:22
  • OfStack

This paper gives an example of how Android programming uses Intent to pass objects. To share for your reference, the details are as follows:

In previous articles, you've seen how Intent can be used to start an activity, send a broadcast, inspire a service, and you can pass 1 bit of data when using Intent. The following code is shown:


Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("info", "I am fine");
startActivity(intent);

When passing data, the method used is putExtra and the data types supported are limited. How do you pass objects?

In Android, there are two ways to pass objects using Intent: Serializable serialization and Parcelable serialization.

1. Serializable method

This means that an object is converted to a storable or transportable state, and the serialized object can be transferred over the network or stored locally.

Object serialization, you only need to implement the Serializable class.


package com.example.testapplication;
import java.io.Serializable;
/**
 *  Object serialization 
 * @author yy
 *
 */
public class Emp implements Serializable {
  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;
  }
}

So how does Intent pass object parameters? Look at API and find the following:

intent.putExtra(String name, Serializable value);

Therefore, use this method to pass as follows:


Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("obj", new Emp());
startActivity(intent);

So how do you get it? Use the following methods:

Emp emp = (Emp) getIntent().getSerializableExtra("obj");

This gives you the Emp object.

2. Parcelable method

This approach is implemented by splitting a complete object so that each part of the split is a data type supported by Intent. Examples are as follows:


package com.example.testapplication;
import android.os.Parcel;
import android.os.Parcelable;
/**
 * Parcelable way 
 * @author yy
 *
 */
public class Emp2 implements Parcelable{
  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;
  }
  @Override
  public int describeContents() {
    return 0;
  }
  @Override
  public void writeToParcel(Parcel dest, int flag) {
    // Write out the name
    dest.writeString(name);
    // Write out the age
    dest.writeInt(age);
  }
  public static final Parcelable.Creator<Emp2> creator = new Creator<Emp2>() {
    @Override
    public Emp2[] newArray(int size) {
      return new Emp2[size];
    }
    @Override
    public Emp2 createFromParcel(Parcel source) {
      Emp2 emp2 = new Emp2();
      // Read in the same order as written above 1 to 
      // read name
      emp2.name = source.readString();
      emp2.age = source.readInt();
      return emp2;
    }
  };
}

Transfer object: in the same way as serialization:


Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("obj", new Emp2());
startActivity(intent);

Get object:

Emp2 emp2 = getIntent().getParcelableExtra("obj");

3, the difference between

Serializable generates a large number of temporary variables when serialized, causing frequent GC. Therefore, Parcelable performs better than Serializable when using memory, so it is recommended to use the Parcelable class.

Parcelable cannot be used in cases where data is to be stored on disk because Parcelable does not do a good job of ensuring data persistence in the event of external changes. Although Serializable is inefficient and not recommended, in this case, Serializable is recommended.

I hope this article has been helpful in Android programming.


Related articles: