Android Development
Object Transfer
Activity Navigation
Android Coding Tips
Android Activities

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Passing an object from one Android activity to another usually means putting that object into an Intent extra. Primitive values are easy, but custom objects need a serialization mechanism that Android can place into a Bundle and reconstruct in the destination activity.

Prefer Parcelable for Android Objects

Parcelable is the Android-native approach and is generally preferred over Serializable for app components because it is designed for Bundle and Intent transport.

Here is a simple Java model class:

java
1package com.example.app;
2
3import android.os.Parcel;
4import android.os.Parcelable;
5
6public class User implements Parcelable {
7    private final String name;
8    private final int age;
9
10    public User(String name, int age) {
11        this.name = name;
12        this.age = age;
13    }
14
15    protected User(Parcel in) {
16        name = in.readString();
17        age = in.readInt();
18    }
19
20    public static final Creator<User> CREATOR = new Creator<User>() {
21        @Override
22        public User createFromParcel(Parcel in) {
23            return new User(in);
24        }
25
26        @Override
27        public User[] newArray(int size) {
28            return new User[size];
29        }
30    };
31
32    public String getName() {
33        return name;
34    }
35
36    public int getAge() {
37        return age;
38    }
39
40    @Override
41    public int describeContents() {
42        return 0;
43    }
44
45    @Override
46    public void writeToParcel(Parcel dest, int flags) {
47        dest.writeString(name);
48        dest.writeInt(age);
49    }
50}

Once the class implements Parcelable, you can send it through the intent that launches the next activity.

Sending the Object

In the source activity:

java
1package com.example.app;
2
3import android.content.Intent;
4import android.os.Bundle;
5
6import androidx.appcompat.app.AppCompatActivity;
7
8public class MainActivity extends AppCompatActivity {
9
10    @Override
11    protected void onCreate(Bundle savedInstanceState) {
12        super.onCreate(savedInstanceState);
13
14        User user = new User("Maya", 28);
15
16        Intent intent = new Intent(this, DetailActivity.class);
17        intent.putExtra("user_extra", user);
18        startActivity(intent);
19    }
20}

The key "user_extra" is just a string identifier. In a larger codebase, define such keys as constants to avoid typos.

Receiving the Object

In the destination activity:

java
1package com.example.app;
2
3import android.os.Bundle;
4import android.widget.TextView;
5
6import androidx.appcompat.app.AppCompatActivity;
7
8public class DetailActivity extends AppCompatActivity {
9
10    @Override
11    protected void onCreate(Bundle savedInstanceState) {
12        super.onCreate(savedInstanceState);
13
14        TextView textView = new TextView(this);
15        setContentView(textView);
16
17        User user = getIntent().getParcelableExtra("user_extra");
18
19        if (user != null) {
20            textView.setText(user.getName() + " is " + user.getAge() + " years old.");
21        }
22    }
23}

That is the full round trip. The object is written into the intent extras, then rebuilt in the second activity.

What About Serializable

Serializable also works and takes less code:

java
1public class User implements java.io.Serializable {
2    private final String name;
3    private final int age;
4
5    public User(String name, int age) {
6        this.name = name;
7        this.age = age;
8    }
9}

You would then use putExtra and getSerializableExtra. That approach is simpler for quick prototypes, but Android developers usually choose Parcelable for component-to-component transport because it is the platform-friendly option.

Keep the Payload Small

Passing an object through an intent is best for small, self-contained data. If the object contains large images, long lists, or database-sized payloads, pass an identifier instead and reload the real data in the next activity.

For example, send a user ID and let the destination activity fetch the full record from a repository or ViewModel. This is more robust across process death and configuration changes.

Common Pitfalls

The most common bug is forgetting to implement the parcel read and write logic in the same order. If you write name first and age second, you must read them back in that exact sequence.

Another issue is using different keys in the sending and receiving activities. "user_extra" and "user" are not the same key, and the destination will receive null.

A third mistake is passing very large objects. Intents and bundles are not meant to carry heavy application state, and oversized payloads can cause crashes or lifecycle problems.

Finally, avoid using singletons as a hidden transport channel between activities. They can appear to work at first, but they do not survive process recreation reliably and often make state harder to reason about.

Summary

  • The common Android solution is to pass custom objects through an Intent extra.
  • 'Parcelable is generally preferred over Serializable for Android component communication.'
  • Write and read parcel fields in the same order.
  • Use a shared constant for the extra key to avoid typos.
  • Pass lightweight objects only; for large state, send an ID and reload the data in the destination activity.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.