Android Parcelable example – Passing Objects via Bundle!

In this article, we are going to look at Android Parcelable example by passing objects via bundle between activities and fragments. Although there are no prerequisites for this tutorial, here I am going to take the Movie Board project that we’ve been building in our previous posts and implement Parcelable to it and pass the objects via bundle.

The completed project can be downloaded from the following github repository link:

Android Parcelable Example

So lets dive into the required steps to complete the Android Parcelable Example.

1. Making our Model Class implement Android Parcelable:

Our previous project of Movie Board consists of a Model class named Movie.java which consists of the following code previously:

Now lets implement Android Parcelable class to the above class so that we can pass the Object for the Movie class via Bundle between activities/ fragments.

The above class after implementing Android parcelable will be as follows:

2. Creating our second screen:

Now lets create our second screen for which we will pass our data into.

First, I am going to create a layout using our previous example on Android Collapsing Toolbar Layout. I am going to name the required layout files as content_collapsible_toolbar.xml and movie_detail_fragment.xml and write the following codes to them.

content_collapsible_toolbar.xml:

movie_detail_fragment.xml:

Lets wire the above layout in java code by using a class extending Fragment, named MovieDetailFragment.java and write the following code:

3. Implementing Listener to go to second screen:

The only thing remaining is to implement a touch listener to our recyclerview so that we can navigate to second screen along with the bundle which consists Movie Object of the recycler item which was clicked.

Lets call the method addOnItemTouchListener of our RecyclerView in our MainActivity.java.

Notice how bundle is created and is passed from activity to fragment. Now my Complete Activity will be as follows:

MainActivity.java:

 

Run the app now to see that the object is passed from Activity to Fragment using Android Parcelable!

Vamsi Tallapudi
Connect Via

Vamsi Tallapudi

Founder and Author at Code Refer
Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook 🙂
Vamsi Tallapudi
Connect Via

Related Posts

Vamsi Tallapudi

Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook :)

You may also like...