Pass custom object from one activity to another

If you have just started learning to create android apps, probably you are playing a lot around activities . Creating new activities , starting one activity from another , passing values between activities etc . Well, have you ever tried sending your custom java objects to an activity ? The way you did it for an int or string does not work here , right! In this tutorial you’ll learn how to do that in the following steps .

Steps:

  • 1.Create your java bean class.
  • 2.Make your class implement Parcelable.
  • 3.Use putExtra(String name, Parcelable value) to add it to the intent in ActivityA.
  • 4.Get your custom object in ActivityB using intent.getParcelableExtra(String nam)

1.Create your java bean class.
Let us consider a simple class User

2.Make your class implement Parcelable.

3.Use putExtra(String name, Parcelable value) to add it to the intent in ActivityA.
Now we can use intent.putExtra(“user”,user) as User implements Parcelable.

4.Get your custom object in ActivityB using intent.getParcelableExtra(String nam)
To retrieve the user object from intent in the receiving activity we shall use getIntent().getParcelableExtra(“user”);

Leave a Reply

Your email address will not be published. Required fields are marked *