Implementing the Parcelable Interface in Android

Implementing the Parcelable Interface in AndroidAnimesh RoyBlockedUnblockFollowFollowingApr 22Passing data between activities outside its parent class is difficult to new Android Developer who is not very comfortable with android concepts.

But once understand it, This will become very easy for you.

I am going to divide this article into three parts by explaining:1.

What is Parcelable?2.

Why use Parcelable over Serializable?3.

Different ways you can pass data between activities.

The question you need to ask yourself first is whether or not you want to make the data persistent.

You can achieve the data persistently in various ways like shared preferences or by using a database but it will not cover in this article.

In this article we only concerned about passing data between activities without making them persistent.

What is Parcelable?We all use Intent to move from one activity to another.

In this process, we use the putExtra and getExtra method of our intent object.

Suppose we want to pass a String or Boolean or Integer or Double value from the first activity to second activity and to display thosevalue into a TextView in the second activity.

We can easily do that via the putExtra method of our intent object or using bundle.

Let me give you an example of how you can achieve this by using the putExtra method of our intent object (not by the bundle, I willshow you can do that by using Bundle as well):This works very well for the basics datatype such as String, Integer, Boolean, Double like in the above example.

But this doesn’twork for objects.

We cannot pass the objects as we have done in the same way.

So there comes the Serialization and Parcelable to solves this problem.

Why use Parcelable over Serializable?Parcelable is an interface and it’s part of the Android SDK.

Serializable is also an interface and it’s not a part of Android SDK.

Parcelable is the Android implementation of Java serializable.

Parcelable is relatively fast as compared to the java serialization.

It’s recommended to use Parcelable.

Different ways you can pass data between activities:1.

Passing data between activities using Intent:Passing Data Between Activities Android TutorialTransfer data between different activity is a common use case in the Android application.

This example will tell you how to…www.



Bundle MethodAndroid has a class called Bundle where we can store our data and it supports several data types like strings, chars, boolean, integer and so on.

Instead of using the Direct Intent as a data container, we can store our data directly into the bundle and then save the bundle into the Intent.

Example Code(passing data from one activity to another using bundle):animeshroydev/UdacityPracticalQuizGoogle India Challange Scholarship 2018 #PracticalQuiz1 – animeshroydev/UdacityPracticalQuizgithub.


SharedPreferences or by using database.

Implement Parcelable:Add the required variable, constructor and getter.

Implement the interface and override the required methods to all the classes you want to transfer data between activities.

writeToParcel Method:In this method, you need to add all the properties to a parcel which you want to transfer.

You use write methods to add each of the properties.

Constructor used to reading and saved values from the parcel:CREATOR used for unparcelling the parcel (creating the object).

This method is to bind everything togetherAt this point, you have populated the object with data.

describeContents() method:This method returns the hashcode of the object.

This method does not do too much.

Receiving all the values in the Second Activity:Now in the second activity, we need to collect the intent object and extract the property Once we have done that we can call the method of name, age, phone.




java (implement Parcelable)Screenshot of the app built in the above example:Demo AppThe full source code of the above screenshot:animeshroydev/ParcelableDemoTransfer name, age, phone number from FirstActivity to SecondActivity by implementing android Parcelable interface.


comPlease let me know if you have any comments or questions.

Thanks and Happy Coding! :).

. More details

Leave a Reply