Passing Objects Between Android Activities: Serializable vs. Parcelable

Introduction

In Android development, passing data between activities is a common task. When dealing with custom objects, there are two primary methods to achieve this: using Serializable and implementing the Parcelable interface. This tutorial explores both approaches, highlighting their differences, use cases, and best practices.

Understanding Serializable

The Serializable interface in Java allows an object’s state to be converted into a byte stream, making it easy to pass objects between activities. It is part of the standard Java API, requiring no additional methods beyond implementing the interface itself.

Example: Using Serializable

  1. Define a Serializable Class

    To use Serializable, ensure your class implements this interface:

    import java.io.Serializable;
    
    public class Customer implements Serializable {
        private String firstName, lastName, address;
        int age;
    
        public Customer(String fname, String lname, int age, String address) {
            this.firstName = fname;
            this.lastName = lname;
            this.age = age;
            this.address = address;
        }
    
        // Getter methods
    }
    
  2. Passing the Object

    In your sending activity:

    Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
    Customer customer = new Customer("John", "Doe", 30, "123 Street");
    intent.putExtra("customerObject", customer);
    startActivity(intent);
    
  3. Retrieving the Object

    In your receiving activity:

    Intent intent = getIntent();
    Customer customer = (Customer) intent.getSerializableExtra("customerObject");
    

Pros and Cons of Serializable

  • Pros: Simple to implement, requires no additional coding beyond implementing Serializable.
  • Cons: Not optimized for Android; can be slower due to reflection.

Understanding Parcelable

Parcelable is an Android-specific interface designed for high-performance inter-process communication. It allows you to serialize and deserialize objects efficiently.

Example: Using Parcelable

  1. Define a Parcelable Class

    Implement the Parcelable interface:

    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class Customer implements Parcelable {
        private String firstName, lastName, address;
        int age;
    
        // Constructor
        public Customer(String fname, String lname, int age, String address) {
            this.firstName = fname;
            this.lastName = lname;
            this.age = age;
            this.address = address;
        }
    
        protected Customer(Parcel in) {
            firstName = in.readString();
            lastName = in.readString();
            address = in.readString();
            age = in.readInt();
        }
    
        public static final Creator<Customer> CREATOR = new Creator<Customer>() {
            @Override
            public Customer createFromParcel(Parcel in) {
                return new Customer(in);
            }
    
            @Override
            public Customer[] newArray(int size) {
                return new array[size];
            }
        };
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(firstName);
            dest.writeString(lastName);
            dest.writeString(address);
            dest.writeInt(age);
        }
    }
    
  2. Passing the Object

    In your sending activity:

    Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
    Customer customer = new Customer("John", "Doe", 30, "123 Street");
    intent.putExtra("customerObject", customer);
    startActivity(intent);
    
  3. Retrieving the Object

    In your receiving activity:

    Intent intent = getIntent();
    Customer customer = intent.getParcelableExtra("customerObject");
    

Pros and Cons of Parcelable

  • Pros: Faster than Serializable due to manual serialization; optimized for Android.
  • Cons: More code-intensive, requiring boilerplate methods.

Conclusion

Choosing between Serializable and Parcelable depends on your specific needs. For simplicity and when performance is not critical, Serializable is a suitable choice. However, for optimal performance in Android applications, especially with complex objects or large data sets, Parcelable is recommended.

Best Practices

By understanding the strengths and weaknesses of each method, you can make informed decisions that best suit your application’s requirements.

Leave a Reply

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