Quantcast
Channel: Envato Tuts+ Code - Mobile Development
Viewing all articles
Browse latest Browse all 1836

How to Pass Data Between Activities With Android Parcelable

$
0
0

Introduction

We often need to pass data between Activities of an Android app. An easy way to do this is with Intent.putExtra(), but if you have a lot of structured data to pass, Parcelable may be a better solution. In this post I'll show you how Parcelable makes it easy to serialize classes for sharing between Activities.

Why Parcelable?

Parcelable is an Android-only Interface. It allows developers to serialize a class so its properties are easily transferred from one activity to another. This is done by reading and writing of objects from Parcels, which can contain flattened data inside message containers. 

Creating the Main Activity and Layout

Our main Activity will handle the collection of the book details. Let's start by setting up our onCreate method.

Next, open activity_main.xml to set up the view's layout and appearance. You will need two text entry fields and a button for submission.

It should look like this:

Now open your main Activity and link the view fields to your activity. You'll have to do it inside your onCreate() method, like this:

To finish off MainActivity, you need to set up an onClickListener. This will be called whenever the Submit button is pressed. When that happens, the details entered are to be collected and sent to the next activity.

Here, you add an onClickListener to the Button instance you retrieved from your Activity layout. This code will be run whenever the Submit button is clicked. 

Note that we simply pass the Book instance to putExtra(). As we'll see later, Parcelable takes care of serializing the book data to a string so it can be passed via the Intent.

Now that the main Activity is complete, we need to create our BookActivity as well as the Book class to hold book info.

Create the Book Class

Let's create a Book class to hold info about each book.

This class needs to implement Parcelable. This will enable the passing of the data from MainActivity to BookActivity.

We'll also add some standard getter functions and a constructor to quickly create an instance of the class.

Write to the Parcel

The writeToParcel method is where you add all your class data to the parcel. This is done in preparation for transfer. This method will be passed a Parcel instance which has a number of write methods that you can use to add each field to the parcel. Watch out: the order in which you write the data is important!

Here is how you do it.

Read Data Back From the Parcel

Just as the write method handles writing the data to be transferred, the constructor is used to read the transferred data back from the serialized Parcel. This method will be called on the receiving activity to collect the data.

Here's how it should look:

The receiving Activity will get the data as a string, and will then call the getParcelableExtra method to start the process of collecting the data. That will trigger the constructor we defined above, which will deserialize the data and create a new Book instance. 

Parcelable.Creator

To complete your Parcelable class, you need to create a Parcelable.Creator instance and assign it to the CREATOR field. The Parcelable API will look for this field when it needs to deserialize an instance of your class that has been passed to another component.

This binds everything together. Its job is simple—it generates instances of your Parcelable class from a Parcel using the parcel data provided. The creator calls the constructor you defined above, passing it a Parcel object, and the constructor initializes the class attributes.

If your Parcelable class will have child classes, you'll need to take some extra care with the describeContents() method. This will let you identify the specific child class that should be created by the Parcelable.Creator. You can read more about how this works on Stack Overflow.

Book Activity and Layout

Now we can complete our app with the book Activity. Go ahead and create a new empty activity called BookActivity. Make the layout look like what I have below.

In the activity_book.xml, you only need two TextView widgets, which will be used to show the title and author of the books.

Now let's set up our activity. Your activity should already look like this:

In this activity, you want to receive the data that was passed from your main Activity and display it on your views. Thus you will retrieve the instances of your TextView using the id of the TextView set in your layout.

Then, of course, you'll call getIntent() because you will be retrieving data in this activity. The data you will be retrieving are collected from the Book class using getParcelableExtra(). Next, you set the values of the TextViews to the data you collected. Here is how it is done.

Build your application and launch it, and you should see the little beauty you have just built.

The completed app

Conclusion

In this post, you've seen how you can easily move objects from one activity to another. You no longer have to retrieve each data field you passed to the Intent object separately, and you don't have to remember the name that you passed each field under. Not only that, but this process is faster than Java's serialize functionality.

In this tutorial, you have learned how to use Parcelable to pass data from one activity to another. You can dive deeper into Parcelable by checking the official documentation

In the meantime, check out some of our other posts about Android app development!

2017-10-02T15:00:00.000Z2017-10-02T15:00:00.000ZChinedu Izuchukwu

Viewing all articles
Browse latest Browse all 1836

Trending Articles