Android Java

Android Parcelable: Effortless Data Transfer Between Activities10 min read

Hello and welcome! Today, we’re going to talk about a commonly encountered topic in the world of Android development: ‘Parcelable’.

In Android, data transfer is quite common. We often come across situations where we need to send data from one activity or fragment to another, or pass a data model from one activity to another. In such data transfer operations, the data needs to be serialized and deserialized. This is where Parcelable comes into play.

Parcelable is an interface used in the Android platform, which allows an object to be broken down into parts and then reassembled into another object. Parcelable can be thought of as an alternative to Serializable, but it is more performant and optimized.

Step 1: Setting up the Project

To begin, let’s set up our project in Android Studio. Open Android Studio and create a new project, providing a suitable project name, package name, and selecting the minimum SDK version based on your target audience.

Step 2: Designing the Layout

Before creating the Parcelable class, let’s start by designing the layout as shown in the video. We’ll need to create a layout that includes three EditText objects for gathering user input. Additionally, we’ll add a button to facilitate the transition between activities.


Step 3: Creating the Parcelable Class

Once the layout is ready, we can proceed to create the Parcelable class. This class will represent the data we want to transfer between components. We’ll define the necessary properties, such as the fields to hold the values from the EditText objects. We’ll also include a constructor and the required getter and setter methods.

After defining the Parcelable class, we’ll implement the Parcelable interface by adding the necessary methods: ‘writeToParcel’ and ‘createFromParcel’. These methods will handle the serialization and deserialization of our object.

Now that our Parcelable class is complete, we can move on to the next steps of the tutorial.

Step 4: Using Parcelable in Activities or Fragments

Now that we have our Parcelable class ready, we can use it to transfer data between activities or fragments. Let’s see how we can achieve this.

In our code, we have defined EditText and Button objects, namely ‘edFirstName’, ‘edLastName’, ‘edAge’, and ‘btnSend’. We retrieve references to these UI elements using the ‘findViewById’ method.

Next, we set an onClickListener on the ‘btnSend’ button. When the button is clicked, the onClick method is triggered. Inside this method, we retrieve the values entered by the user from the EditText objects, ‘edFirstName’, ‘edLastName’, and ‘edAge’.

Using these values, we create a new instance of our Parcelable class, ‘User’, and pass the collected data as parameters to its constructor.

To transfer this Parcelable object to another activity, we create an intent by specifying the current activity, ‘MainActivity’, and the target activity, ‘UserActivity’. We add the Parcelable object as an extra to the intent using the ‘putExtra’ method, with a key-value pair where the key is ‘user’ and the value is our Parcelable object.

Finally, we start the ‘UserActivity’ by calling ‘startActivity(intent)’, which initiates the transition to the receiving activity, along with the Parcelable data.

In the receiving activity, we can retrieve the Parcelable object from the intent by using the ‘getParcelableExtra’ method, passing the same key ‘user’. This allows us to access and utilize the transferred data within the receiving activity.

That’s how we use Parcelable to transfer data between activities or fragments in our Android application. Feel free to explore further and implement Parcelable for efficient data transfer in your own projects.

Step 5: Designing the User Activity

Once we have designed our user interface, let’s take a look at the layout file. We have used the ConstraintLayout as the root element and added three TextViews: textViewFirstName, textViewLastName, and textViewAge. These TextViews will display the corresponding data we receive through Parcelable.

With the help of constraints, we have positioned the TextViews on the screen, ensuring proper alignment. The layout_width and layout_height attributes are set to ‘wrap_content’ to adjust the size based on the content.


In just a few simple steps, we have created a visually appealing layout for our UserActivity. Now, let’s move forward and handle the data received from the previous activity.

That’s all there is to it! Parcelable is a powerful tool in Android development that helps in efficient data transfer between components. Now, let’s see how we can retrieve and display the transferred data in our UserActivity.

Step 5: Unpacking Data in User Activity

In our code, we have defined TextView objects: tvFirstName, tvLastName, and tvAge. We retrieve references to these TextViews using the ‘findViewById’ method.

Next, we obtain the intent that started the UserActivity using the ‘getIntent’ method. We then retrieve the Parcelable object, ‘User’, from the intent using the ‘getParcelableExtra’ method, specifying the key ‘user’.

To display the data, we use the setText method on each TextView, setting the values from the User object. ‘tvFirstName’ displays the first name, ‘tvLastName’ displays the last name, and ‘tvAge’ displays the age.

With these simple steps, we have successfully retrieved the Parcelable data and displayed it in our UserActivity.

That concludes our tutorial on using Parcelable for efficient data transfer. I hope you found this video informative and helpful. If you have any questions or suggestions, please feel free to leave them in the comments section below. Thank you for watching, and don’t forget to like and subscribe for more Android development tutorials!”

Feel free to make any adjustments or modifications to the script as per your preference.

Leave a Comment