android parcelable

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 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.



Lets wire the above layout in java code by using a class extending Fragment, named 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

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

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

Related Posts