Introduction
In Android development, communication between different components of an application, such as activities, is a fundamental requirement. One common scenario involves passing data from one activity to another. This tutorial focuses on transferring custom objects between activities using intents by implementing either the Parcelable
or Serializable
interface.
Overview
An Intent
in Android serves as a messaging object carrying information between components. When it comes to sending custom objects, you can either implement the Parcelable
or Serializable
interfaces. Each method has its advantages and trade-offs. The choice often depends on performance considerations and complexity of the data being passed.
Using Parcelable
The Parcelable
interface is Android’s recommended way for marshalling class objects as it offers better performance than serialization due to reduced overhead. Implementing Parcelable
involves several steps:
Steps to Implement Parcelable
-
Add Required Methods: Your custom object must implement the
Parcelable
interface, overriding necessary methods. -
Implementing
writeToParcel
Method:- This method writes the object’s data to a
Parcel
. You need to manually handle the serialization of all fields in your class.
- This method writes the object’s data to a
-
Create a CREATOR Field:
- A static field named
CREATOR
, implementing theParcelable.Creator
interface, is mandatory. It helps recreate the object from its parcel representation.
- A static field named
-
Constructor for Parcel:
- Implement a constructor that takes a
Parcel
and initializes fields using data extracted from it.
- Implement a constructor that takes a
Example Code
public class MyParcelable implements Parcelable {
private int mData;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Creator<MyParcelable> CREATOR = new Creator<MyParcelable>() {
@Override
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
@Override
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
Sending and Receiving Parcelable Objects
To send a Parcelable
object via an intent:
MyParcelable myObject = new MyParcelable();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("myData", myObject);
startActivity(intent);
In the receiving activity, extract it as follows:
Intent intent = getIntent();
MyParcelable receivedObject = (MyParcelable) intent.getParcelableExtra("myData");
Using Serializable
The Serializable
interface is simpler but slower than Parcelable
. It automatically serializes and deserializes objects.
Implementing Serializable
- Implement the Interface: Your class implements
java.io.Serializable
. - No Additional Methods Required: Unlike
Parcelable
, no additional methods or fields are necessary unless you want to customize the serialization process.
Example Code
public class MySerializable implements Serializable {
private static final long serialVersionUID = 1L;
private int mData;
// getters and setters
}
Sending and Receiving Serializable Objects
To send a Serializable
object:
MySerializable myObject = new MySerializable();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("myData", myObject);
startActivity(intent);
To receive it:
Intent intent = getIntent();
MySerializable receivedObject = (MySerializable) intent.getSerializableExtra("myData");
Best Practices
-
Choose Between Parcelable and Serializable: Use
Parcelable
for Android-specific components due to its performance benefits. UseSerializable
when interoperability with Java EE or ease of implementation is more critical. -
Ensure Consistency in Field Order: When using
Parcelable
, ensure that fields are marshalled and unmarshalled in the same order. -
Handle Null Safely: Always check for nulls to prevent potential crashes when retrieving extras from intents.
Conclusion
Passing objects between activities is a common task in Android development. By implementing either the Parcelable
or Serializable
interfaces, developers can efficiently send custom data across components using intents. This guide provides you with the knowledge to choose and implement the appropriate method for your application’s needs.