Passing data between activities

One of the most important aspect in developing android application is passing data between activities. Usually an Android app is made by several activities that work together. These activities are pieces of code independent each other and we need to exchange information between them.

There are several strategies that can be applied to achieve this aim:

Shared Preferences

Database

Static class

Intents

The first two options are valid when we need to pass data and at the same time make this information persistent. This is the case of configuration parameter for example where we have to persist this information to not ask again it. We will cover this topic in another post, by now we are more interested on sharing data between activities without making them persistent. So we are interested on last two options.

Static class

This is the easiest way to achieve our goal. Exploiting the Android feature that an app runs in a single process we can use a static class to exchange data between activities. In this case we can create several static method to hold the data we want to share.
Otherwise, we can use the singleton pattern that ensures we have only one instance of this class in all our JVM. In this case we create several method to hold the data.

Passing data: Intents

This method is the most elegant one because it uses all the android features. Intent in android is an action description.For more information look here.Intent supports three ways to pass data:

Direct: put our data into intents directly

Bundle: create a bundle and set the data here

Parcelable: It is a way of “serializing” our object.

To analyse these methods let’s suppose we have two activities: one that allows user to enter data (EditActivity) and the other one that shows to the user the data (ViewActivity). We have to pass this data between our activities. Let’s suppose, moreover, that we want to pass the name, surname and the email.

Passing data: Direct

Intent has several method called putExtra(String name, …..) that allows us to save inside the Intent our information. Reading the API Doc we know that we can add string, longs, CharSequence, int and so on.It behaves like a Map where there’s the key and the value. In the caller (EditActivity) we can pass data in this way:

Where “name”, “surname” and “email” are the keys. In other word first we create the intent (line 1) defining the caller activity (EditActivity) and the destination activity (ViewActivity), then we use putExtra to add our data. In the destination activity to retrieve the data sent we have:

As we can see this is very easy way to pass data and can be used when we have primitives or very simple object to share.

Bundle

Android has a class called Bundlewhere we can store our data and supports several data types like strings, chars, boolean, integer and so on.
Instead of using the Intent,as data container, we store our info directly into the bundle and then we save the bundle into the Intent. This approach is similar to the one described above.
In this case in the caller activity we have:

Passing data: Parcelable

This is the most elegant way to pass data between activities and exploit all the android power. The “parcelable” operation is like serialization in Java, but while the java serialization in android is inefficient the “parcelable” operation is much more faster.Parcelable in android is an interface and every object that wants to be passed across different activities using intent has to implement this interface. It is something like Serializable in Java. This interface has two method that we have to implements: describeContent() that returns an int and writeToParcel(Parcel dest, int flags) that returns a void.
More over a class that implements this interface must have a static field called CREATOR that is used by the OS to recreate the object.
This strategy is very useful when we need to pass complex objects and of course we can’t use the first two strategies. Let’s suppose we have a class called Person that contains three attributes:name, surname and email.

Let’s analyze the code shown above. The first thing we did is to declare that our class implements Parcelable, so we have to override two method as stated above. The first one is describeContents() where we simply return the hashcode of our class. The second is writeToParcel. In this method we write the attribute values into the Parcel class. We use the Parcel methods to store our information. The order used to write them isn’t important, but we have to use the same order when we have to read it.

Once we have declared that our class is Parcelable, we have to implement a static field called CREATOR. This field is used by OS to “unparcel” or “deserialize” our class. This static field looks like that

There are two other methods to override the most important one is createFromParcel where we receive a parcel and we have to build up our Object, in our case Person. We call in this method a constructor, we made, that read data from the Parcel object:

About The Author

I'm an electronic engineer with over 15 years of
experience in computer programming. I'm the author of the book "Android things projects". I'm a MVB at DZone and DZone Guide Author. I have contributed to the IoT guide. I'm the technical reviewer of the book Internet of Things with Arduino Blueprints(published by PacktPub). I love creating IoT projects using Arduino, Raspberry Pi,
Android, and other platforms. I am interested in the convergence
between IoT and the mobile applications. I'm SCEA, SCWCD, and SCJP
certified.
Follow on: Twitter | Linkedin

Sign up for our Newsletter

Enter your email and stay on top of things,

Get SwA updates delivered to your inbox. Article, tips, source code

×

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.