How to Send an Object from One Android Activity to Another Using Intents

How to Send an Object from One Android Activity to Another Using Intents

22 May 2024 Stephan Petzl Leave a comment Tech-Help

Passing data between activities in Android is a common task, especially when dealing with complex objects. This guide will explain the best practices for sending an object of a custom type from one Activity to another using the putExtra() method of the Intent class.

Using Parcelable

The recommended way to pass objects between activities is by implementing the Parcelable interface. This method is preferred because it is much faster than Java’s native serialization.

Step-by-Step Implementation

  1. Create a Parcelable Class: Implement the Parcelable interface in your class.
  2. 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 Parcelable.Creator CREATOR = new Parcelable.Creator() {
            public MyParcelable createFromParcel(Parcel in) {
                return new MyParcelable(in);
            }
    
            public MyParcelable[] newArray(int size) {
                return new MyParcelable[size];
            }
        };
    
        private MyParcelable(Parcel in) {
            mData = in.readInt();
        }
    }
  3. Put the Parcelable Object in the Intent: Use the putExtra() method to add your Parcelable object to the Intent.
  4. Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("my_parcelable", myParcelableObject);
    startActivity(intent);
  5. Retrieve the Parcelable Object: In the receiving activity, use the getParcelableExtra() method to retrieve your object.
  6. Intent intent = getIntent();
    MyParcelable myParcelableObject = intent.getParcelableExtra("my_parcelable");

Using Serializable

An alternative to Parcelable is to implement the Serializable interface. This method is simpler but less efficient than Parcelable.

Step-by-Step Implementation

  1. Create a Serializable Class: Implement the Serializable interface in your class.
  2. public class MySerializable implements Serializable {
        private int mData;
    }
  3. Put the Serializable Object in the Intent: Use the putExtra() method to add your Serializable object to the Intent.
  4. Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("my_serializable", mySerializableObject);
    startActivity(intent);
  5. Retrieve the Serializable Object: In the receiving activity, use the getSerializableExtra() method to retrieve your object.
  6. Intent intent = getIntent();
    MySerializable mySerializableObject = (MySerializable) intent.getSerializableExtra("my_serializable");

Using JSON with GSON

For developers who prefer working with JSON, Google’s GSON library offers another way to pass objects between activities.

Step-by-Step Implementation

  1. Convert Object to JSON: Use GSON to convert your object to a JSON string.
  2. Gson gson = new Gson();
    String jsonString = gson.toJson(myObject);
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("my_json", jsonString);
    startActivity(intent);
  3. Retrieve JSON and Convert Back to Object: In the receiving activity, retrieve the JSON string and convert it back to your object using GSON.
  4. Intent intent = getIntent();
    String jsonString = intent.getStringExtra("my_json");
    MyObject myObject = gson.fromJson(jsonString, MyObject.class);

Conclusion

Choosing the right method depends on your specific requirements. Parcelable is generally the best choice for its performance, but Serializable and JSON with GSON are also viable options, especially for simpler use cases.

Automating Your Tests with Repeato

While developing and testing your Android applications, consider using Repeato, a No-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests for your apps quickly and efficiently, leveraging computer vision and AI. This tool is particularly beneficial for mobile developers as it helps to focus on creating a great product without getting bogged down by the complexities of test automation. Non-technical colleagues or QAs can also handle test automation tasks, streamlining the workflow even further.

For more detailed guides and documentation, visit our documentation page.

Like this article? there’s more where that came from!