How to pass objects between Android Activities?

Neeraja Gandla
5 min readMar 15, 2020

--

I get asked this question by new android developers. So I thought of writing a blog post on various ways to send data between activities. Though there are various blog posts on this topic, this article portrays my understanding of many such resources.

I have been using Gson - TypeToken way to pass objects or list of objects between activities. I felt that’s convenient. But It seems to be not a best practice when I referred various resources online. There are claims that it’s slow when compared to Parcelable.

The most popular ways of passing objects between activities turned out to be using either Serializable or Parcelable implementation for the object.

Using Serializable implementation

Using Serializable is simple. We just have to implement Serializable in the target object that is to be passed between activities and also in any of its member variables. Below is an example:

public class User implements Serializable {
private int id;
private String userName;
private String emailAddress;
private Address address;
private List<Skill> skills;

//getters and setters here
...
}

And since we have Address and List<Skill> as member variables in User class, we also need to implement Serializable in Address and Skill classes.

public class Address implements Serializable {
private String houseNumber;
private String street;
private String locality;
private String area;
private String city;
private int pincode;

//getters and setters here
}
public class Skill implements Serializable {
private String skillName;
private String proficiency;
private int experienceInYears;

//getters and setters here
}

We can now pass the user object across activities as show below:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("user", user); //where user is an instance of User object
startActivity(intent);

And in the SecondActivity :

if(getIntent().getExtras() != null) {
User user = (User) getIntent().getSerializableExtra("user");
}

Using Parcelable implementation

Parcelable is supposedly faster than Serializable but there are counter arguments to it which I won’t be getting into details in this blog post.

public class User implements Parcelable {
private int id;
private String userName;
private String emailAddress;
private Address address;
private List<Skill> skills;

public User() {}

//getters and setters here

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.userName);
dest.writeString(this.emailAddress);
dest.writeParcelable(this.address, flags);
dest.writeTypedList(
this.skills);
}

protected User(Parcel in) {
this.id = in.readInt();
this.userName = in.readString();
this.emailAddress = in.readString();
this.address = in.readParcelable(Address.class.getClassLoader());
this.skills = in.createTypedArrayList(Skill.CREATOR);
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}

@Override
public User[] newArray(int size) {
return new User[size];
}
};
}

public class Skill implements Parcelable {
private String skillName;
private String proficiency;
private int experienceInYears;

public Skill(String skillName, String proficiency, int experienceInYears) {
this.skillName = skillName;
this.proficiency = proficiency;
this.experienceInYears = experienceInYears;
}

protected Skill(Parcel in) {
skillName = in.readString();
proficiency = in.readString();
experienceInYears = in.readInt();
}

public static final Creator<Skill> CREATOR = new Creator<Skill>() {
@Override
public Skill createFromParcel(Parcel in) {
return new Skill(in);
}

@Override
public Skill[] newArray(int size) {
return new Skill[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(skillName);
dest.writeString(proficiency);
dest.writeInt(experienceInYears);
}
}

public class Address implements Parcelable {
private String houseNumber;
private String street;
private String locality;
private String area;
private String city;
private int pincode;

public Address(String houseNumber, String street, String locality, String area, String city, int pincode) {
this.houseNumber = houseNumber;
this.street = street;
this.locality = locality;
this.area = area;
this.city = city;
this.pincode = pincode;
}

protected Address(Parcel in) {
houseNumber = in.readString();
street = in.readString();
locality = in.readString();
area = in.readString();
city = in.readString();
pincode = in.readInt();
}

public static final Creator<Address> CREATOR = new Creator<Address>() {
@Override
public Address createFromParcel(Parcel in) {
return new Address(in);
}

@Override
public Address[] newArray(int size) {
return new Address[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(houseNumber);
dest.writeString(street);
dest.writeString(locality);
dest.writeString(area);
dest.writeString(city);
dest.writeInt(pincode);
}
}

Don’t panic looking at the amount of code. We have to add ‘implements Parcelable’. We can override methods by clicking on the red bulb at the left at the first line of class definition. We just need to take care of what’s in the writeToParcel(..) and protected ClassName(Parcel in) {…} methods. Take a close look at these methods in the User class above. I have intentionally included List<Skill> and Address variables in User class to demonstrate how to write/read them from parcel in writeToParcel(..) and protected ClassName(Parcel in) {…} methods.

If you install Android Parcelable Code Generator Plugin in Android studio, all this code can be auto generated by cmd+N in mac or alt+insert in windows using ‘Parcelable’ option. To see list of available plugins in Android studio, go to Preferences-> plugins.

To pass parcelable objects between activities, we must include the following code:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("user", user);//where user is an instance of User object
startActivity(intent);
//in SecondActivity
if(getIntent().getExtras() != null) {
User user = getIntent().getParcelableExtra("user");
}

To check out how to pass objects between activities using Gson - TypeToken, go through this blog by theblackcat102

As long as you have smaller amounts of data using any of these ways should be fine. However, if there’s large amounts of data Serializable tends to be slower than Parcelable.

Differences between Parcelable and Serializable

Serializable interface is very easy to implement. It is a marker interface and by implementing this, your POJO class is able to be serialized and deserialized. Whereas Parcelable is not a marker interface, hence it has some methods that you will have to override when you implement this in your class.

To know more about marker interfaces in java, I recommend reading this blog post.

Serializable interface is not a part of Android SDK and it uses reflection for marshaling operations and creates lots of temp objects. In Parcelable, you are able to choose which field you want to serialize.

Because of the temp object creation and garbage collection, Serialization is slower than Parcelable.

There are claims that when we customize Serializable implementation, we can make it faster than Parcelable. But I haven’t tried it myself. If you are curious you may explore on how to customize Serialization.

Reference for Additional Reading

Thanks for reading the article. Hope you found it helpful.

Happy Learning. Cheers!

--

--

Neeraja Gandla
Neeraja Gandla

Written by Neeraja Gandla

I’m an Android App Developer by Profession. I would like to write out my thoughts and learning to let others learn through my experiences.

Responses (1)