Android Passing Object Between Activities

In the preceding article I have shown, How to Pass Data Between Activities. In this article I am going to cover how we can Pass an Object between Activities. See the screenshots of an example below.

Android Passing Object Between Activities

Android Passing Object Between Activities

Download Source from here.

In addition passing the data types using putExtra() and putExtras() method, We can also pass the Object using Intent object.
Let’s see how.
Suppose you have your own custom class. For example A Song class. Make sure it should be implements as a Serializable class. See below example of custom Song class.

Now use the putExtra() method to pass an Object to another Activity.

Use the getSerializableExtra() method of Intent Object to retrieve passed object to targeted activity by key you set. And typecast the result to the custom class you created.

See the code below.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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