W3docs

How to pass an object from one activity to another on Android

There are several ways you can pass an object from one activity to another on Android:

There are several ways you can pass an object from one activity to another on Android:

  1. Using Intent Extras: You can put the object in the Intent that you use to start the second activity. To do this, you need to make the object Parcelable (preferred for performance) or Serializable, then use the putExtra method of the Intent class to pass the object.
// Sender Activity
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("my_object", myParcelableObject);
startActivity(intent);

// Receiver Activity
MyParcelableObject obj = getIntent().getParcelableExtra("my_object", MyParcelableObject.class);

Parcelable Implementation Example:

public class MyParcelableObject implements Parcelable {
    private String name;
    // Constructor, getters, and setters omitted for brevity

    protected MyParcelableObject(Parcel in) {
        name = in.readString();
    }

    public static final Creator<MyParcelableObject> CREATOR = new Creator<MyParcelableObject>() {
        public MyParcelableObject createFromParcel(Parcel in) { return new MyParcelableObject(in); }
        public MyParcelableObject[] newArray(int size) { return new MyParcelableObject[size]; }
    };

    @Override public int describeContents() { return 0; }
    @Override public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }
}

Serializable Alternative: Simply implement java.io.Serializable on your class. No boilerplate is required, but it is generally slower and uses more memory than Parcelable.

Pros: Built into Android, lifecycle-safe, and the standard approach for Activity communication. Cons: Requires implementing Parcelable or Serializable; not ideal for very large data sets.

  1. Using a Singleton: You can use a singleton class to store the object, and then retrieve it from the second activity.
public class DataHolder {
    private static DataHolder instance;
    private MyObject data;
    private DataHolder() {}
    public static DataHolder getInstance() {
        if (instance == null) instance = new DataHolder();
        return instance;
    }
    public void setData(MyObject data) { this.data = data; }
    public MyObject getData() { return data; }
}
// Usage: DataHolder.getInstance().setData(obj);
// SecondActivity: MyObject obj = DataHolder.getInstance().getData();

Pros: Simple to implement and globally accessible. Cons: ⚠️ Warning: Can cause memory leaks if it holds Activity or Context references. Discouraged in modern Android development due to lifecycle mismatches.

  1. Using a Static Field: You can create a static field in your first activity and set it to the object you want to pass. The second activity can then access the static field to retrieve the object.
// FirstActivity.java
public static MyObject sharedObject;
// Usage: FirstActivity.sharedObject = obj;

// SecondActivity.java
MyObject obj = FirstActivity.sharedObject;

Pros: Quick to set up with no extra classes required. Cons: ⚠️ Warning: Highly discouraged. Static references prevent garbage collection, leading to memory leaks and unpredictable behavior across configuration changes.

  1. Using a Shared ViewModel (Fragments only): If you are using the Android Architecture Components library, you can use a ViewModel to store the object. Note: ViewModel is scoped to a ViewModelStoreOwner (like an Activity or Fragment) and is intended for sharing data between Fragments within the same Activity, not between separate Activities.
// SharedViewModel.java
public class SharedViewModel extends ViewModel {
    private final MutableLiveData<MyObject> _data = new MutableLiveData<>();
    public LiveData<MyObject> getData() { return _data; }
    public void setData(MyObject obj) { _data.setValue(obj); }
}
// Fragment A: ViewModelProvider(requireActivity()).get(SharedViewModel.class).setData(obj);
// Fragment B: ViewModelProvider(requireActivity()).get(SharedViewModel.class).getData().observe(getViewLifecycleOwner(), obj -> { ... });

Pros: Lifecycle-aware, survives configuration changes, and integrates cleanly with Architecture Components. Cons: Not designed for cross-Activity communication; requires Architecture Components setup.

  1. Using a Database: If the object you want to pass is large or you want to persist it, you can store it in a database and pass the object's ID to the second activity. The second activity can then query the database to retrieve the object.
// Sender Activity
// Assuming a Room DAO or SQLite helper instance
long id = database.insert(myObject);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("object_id", id);
startActivity(intent);

// Receiver Activity
long id = getIntent().getLongExtra("object_id", -1);
MyObject obj = database.getById(id);

Pros: Handles large data efficiently, persists across app restarts, and decouples components. Cons: Overkill for simple passing; requires database setup and introduces query overhead.