Pass Object from One Activity to Another using Serializable in Android – Java

Serializable is a Java Interface through which we can convert a class object into a byte stream. We can pass object from one activity to another using Serializable in Android. Although it is considered a slow method to pass data between activities as compare to Parcelable but it’s easiest to implement. In the following example, you can see how easily we can pass our class objects between the Activities using Serializable Interface.

Implement Serializable Interface in your Model Class

public class UserModel implements Serializable {
    String id;
    String name;
    // Add other propeties & methods
}

Pass Object From One Activity to Another in Intent

UserModel user = new UserModel();
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("user",user);
startActivity(intent);

Receive Object in Second Activity From Intent

Add the following code in onCreate() method.

Intent intent = getIntent();
UserModel user = (UserModel) intent.getSerializableExtra("user");

That’s it. You can use this user object in your second activity.

For other helping, code snippets have a look at our Articles List.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: