Home » Android » Android Core » Android Tutorial : Two methods of passing object by Intent (Serializable,Parcelable)

About Boris Duncan

Boris Duncan
Tc Duncan is a senior developer in Hong Kong. He has 10 years of development experience. In recent year, he is engaged in mobile development.

Android Tutorial : Two methods of passing object by Intent (Serializable,Parcelable)

In this post, I will show you an simple example of how to pass object by intent in Android application. Parcelable and Serialization are used for marshaling and unmarshaling Java objects. In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.

Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparison to Parcelable.

Step 1: main.xml for the layout

<?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     android:text="Hello Welcome to EasyInfoGeek."  

Step 2: Create Person.java which implement serializable

package com.easyinfogeek.objectPass;  
 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 age) {  
  this.age = age;  


Step 3: Create Book.java which implement Parcelable

package  com.easyinfogeek.objectPass;  
 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) {  

Step 4: Create The ObjectPassDemo.java which is the main Activity class Create

2 more activity classes: ObjectPassDemo1.java use for display person ObjectPassDemo2.java use for display book

package com.easyinfogeek.objectPass;  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
public class ObjectTranDemo extends Activity implements OnClickListener {  

    private Button sButton,pButton;  
    public  final static String SER_KEY = "com.easyinfogeek.objectPass.ser";  
    public  final static String PAR_KEY = "com.easyinfogeek.objectPass.par";  
    public void onCreate(Bundle savedInstanceState) {  


    public void setupViews(){  
        sButton = (Button)findViewById(R.id.button1);  
        pButton = (Button)findViewById(R.id.button2);  

    public void SerializeMethod(){  
        Person mPerson = new Person();  
        Intent mIntent = new Intent(this,ObjectTranDemo1.class);  
        Bundle mBundle = new Bundle();  


    public void PacelableMethod(){  
        Book mBook = new Book();  
        mBook.setBookName("Android Developer Guide");  
        Intent mIntent = new Intent(this,ObjectTranDemo2.class);  
        Bundle mBundle = new Bundle();  
        mBundle.putParcelable(PAR_KEY, mBook);  


    public void onClick(View v) {  
        if(v == sButton){  
package com.easyinfogeek.objectPass;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 public class ObjectPassDemo1 extends Activity {  
     public void onCreate(Bundle savedInstanceState) {  

  TextView mTextView = new TextView(this);  
  Person mPerson = (Person)getIntent().getSerializableExtra(ObjectPassDemo.SER_KEY);  
  mTextView.setText("You name is: " + mPerson.getName() + "/n"+  
   "You age is: " + mPerson.getAge());  

package com.easyinfogeek.objectPass;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 public class ObjectPassDemo2 extends Activity {  

     public void onCreate(Bundle savedInstanceState) {  
  TextView mTextView = new TextView(this);  
  Book mBook = (Book)getIntent().getParcelableExtra(ObjectPassDemo.PAR_KEY);  
  mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+  
      "Author is: " + mBook.getAuthor() + "/n" +  
      "PublishTime is: " + mBook.getPublishTime());  

Step 5: Modify AndroidManifest.xml add the two acivityies

<?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  <activity android:name=".ObjectPassDemo"  
   <action android:name="android.intent.action.MAIN" />  
   <category android:name="android.intent.category.LAUNCHER" />  
  <activity android:name=".ObjectPassDemo1"></activity>  
  <activity android:name=".ObjectPassDemo2"></activity>  
     <uses-sdk android:minSdkVersion="7" />  


Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....




  1. Great article. Personally, I would use Serializable unless the object is really big in which case the speed difference would be visible by user.

    I also wonder what would be speed difference if we would implement Externalizable interface (and manually implement methods for reading and writing object) instead of Serializable.

  2. Great guide. I’m actually here to ask you a question about Parcelable: what if I want to make my class that includes a parcelable object parcelable? In other words let’s say I have a class with 3 fields: String name, int id and Location loc. Now I want to make this class Parcelable. How can I do it? So far I did as you showed but for the Location entity I changed it like that:
    public MyClass(Parcel in) {
    name = in.readString();
    id = in.readInt();
    loc = in.readParcelable(Location.class.getClassLoader()); }

    public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(loc, flags); }

    When I go to use it I get and error: Class not found when unmarshalling…

  3. Thanks….got nice way to pass object with the help of Intent ,instead of making object static.

  4. Great article, i will prefer using parcelable because it is faster.

  5. Thanks for the example, there is a typo in the code i think
    Intent mIntent = new Intent(this,ObjectTranDemo1.class); should be
    Intent mIntent = new Intent(this,ObjectPassDemo1.class);

    Intent mIntent = new Intent(this,ObjectTranDemo2.class); should be
    Intent mIntent = new Intent(this,ObjectPassDemo2.class);

  6. Nice article. @Robert I would actually advocate for using Serializable especially if the object is a big one – a big graph of objects.

    I go into details in this blog post and I’d love to hear your comments.


  7. great article. thanks. but I have some problem implementing List of type Objects inside my class which implements Parcelable.
    so how should I send and receive list in my class. please help

  8. Nice example. Thank you

  9. excellent post for this topics soo

Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns