Passing Objects via Intent in Android

Shweta

Hi Coders,

Android developers generally pass data between activities and components. If data is primitive type then it is easy to pass via intent. However passing an object is not as simple as a primitive type. Whenever you want to pass an object, you can either make your objects Parcelable or Serializable. Both approaches are a way to serialize and deserialize an object in android. Let’s look at the both approaches.

Parcelable is an interface. A class who implements Parcelable can write to and read from a Parcel.
You need to follow 3 points to create a Parcelable class.
A Class must implement Parcelable interface
A Class must have a non-null static field CREATOR of a type that implements Parcelable.Creator interface.
Override writeToParcel method and write member variable in Parcel. Make sure to read variables in the same sequence in which they are written in Parcel. Order of read and write matters.

Google Certified Agency

Below is the typical implementation of Parcelable.

1
2
3
4
5
6
7
public class Movie implements Parcelable {
// Member variables
private String title;
private String overview;
private String release_date;
private String original_language;
private int id;

// In constructor you will read the variables from Parcel. Make sure to read them in the same sequence in which you have written them in Parcel.
public Movie(Parcel in) {
title = in.readString();
overview = in.readString();
release_date = in.readString();
id = in.readInt();
original_language = in.readString();
}

// This is where you will write your member variables in Parcel. Here you can write in any order. It is not necessary to write all members in Parcel.
@Override
public void writeToParcel(Parcel dest, int flags) {
// Write data in any order
dest.writeString(title);
dest.writeString(overview);
dest.writeString(release_date);
dest.writeInt(id);
dest.writeString(original_language);
}
// This is to de-serialize the object
public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>(){
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}

public Movie[] newArray(int size) {
return new Movie[size];
}
};
}

Now you are good to pass objects of Movie type from one activity to other activity.

This is how you can pass a Parcelable object using Intent.

1
2
3
4
5
6
7
// In activity or fragment
Movie movie = new Movie();
// using context and next component class to create intent
Intent intent = new Intent(this, NextActivity.class);
// using putExtra(String key, Parcelable value) method
intent.putExtra(“parcel_data”, movie);
startActivity(intent);

You can access this data in NextActivity –

1
2
3
4
5
6
7
public class NextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Using getParcelableExtra(String key) method
Movie movie = (Movie) getIntent().getParcelableExtra("parcel_data");
}
}

Serializable – To serialize an object means to convert its state to byte stream so that it can be read to get the object back. To make a class Serializable, the class needs to implement the Serializable interface. Rest everything is handled by Java. This is how you will make your class Serializable.

1
2
3
4
5
6
7
8
// Implementing Serializable in the previous example only
public class Movie implements Serializable {
private String title;
private String overview;
private String release_date;
private String original_language;
private int id;
}

This is how you will pass a Serializable object –

1
2
3
4
5
6
7
// In activity or fragment
Movie movie = new Movie();
// using context and next component class to create intent
Intent intent = new Intent(this, NextActivity.class);
// using putExtra(String key, Serializable value) method
intent.putExtra(“serialize_data”, movie);
startActivity(intent);

While accessing this data in NextActivity –

1
2
3
4
5
6
7
public class NextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Using getSerializableExtra(String key) method
Movie movie = (Movie) getIntent().getSerializableExtra()("serialize_data");
}
}

Serializable is a marker interface that is why there is no method to implement. Using Serializable is easier than Parcelable. Developer need not to write much code when they use Serializable and even code looks cleaner. Still google recommend to use Parcelable instead of Serializable. Why?

Different between Parcelable and Serializable
Serializable use reflection that is why it is a slow process.
Serializable creates a lot of temporary objects and cause quite a bit of garbage collection.

Conclusion, Parcelable is faster and uses less memory but it requires bit coding and time. Whereas Serialization needs less coding and quick to implement but it is slower and consumes more memory.

Hope this is useful information for you!! If you have any doubt you can post the comment below. Enjoy coding.

Reference links –
https://developer.android.com/reference/android/os/Parcelable.html
https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html

 

free ebook

about the author

Shweta

  1. Eric Jerome

    January 3, 2018

    How to pass data objects using kotlin?

  2. Fabricio

    January 20, 2018

    Thanks man, your article helped me a lot.