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.