Passing custom object between activities

suggest change

It is also possible to pass your custom object to other activities using the Bundle class.

There are two ways:


Parcelable processing is much faster than serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.

public class MyObjects implements Parcelable {
    private int age;
    private String name;
    private ArrayList<String> address;
    public MyObjects(String name, int age, ArrayList<String> address) { = name;
        this.age = age;
        this.address = address;
    public MyObjects(Parcel source) {
        age = source.readInt();
        name = source.readString();
        address = source.createStringArrayList();
    public int describeContents() {
        return 0;
    public void writeToParcel(Parcel dest, int flags) {
    public int getAge() {
        return age;
    public String getName() {
        return name;
    public ArrayList<String> getAddress() {
        if (!(address == null))
            return address;
            return new ArrayList<String>();
    public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() {
        public MyObjects[] newArray(int size) {
            return new MyObjects[size];
        public MyObjects createFromParcel(Parcel source) {
            return new MyObjects(source);

Sending Activity Code

MyObject mObject = new MyObject("name","age","Address array here");

//Passing MyOject 
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObject);

Receiving the object in destination activity.

//Getting MyObjects 
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getParcelable("UniqueKey");

You can pass Arraylist of Parceble object as below

//Array of MyObjects
ArrayList<MyObject> mUsers;

//Passing MyObject List
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);

//Getting MyObject List
Intent mIntent = getIntent();
ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");
Note: There are Android Studio plugins such as this one available to generate Parcelable code


Sending Activity Code

Product product = new Product();
Bundle bundle = new Bundle();
bundle.putSerializable("product", product);
Intent cartIntent = new Intent(mContext, ShowCartActivity.class);

Receiving the object in destination activity.

protected void onCreate(Bundle savedInstanceState) {
    Bundle bundle = this.getIntent().getExtras();
    Product product = null;
    if (bundle != null) {
        product = (Product) bundle.getSerializable("product");

Arraylist of Serializable object: same as single object passing

Custom object should implement the Serializable interface.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents