How to pass a variable to Android activity

Introduction

I’ve started learning Java and Android. I will tell more about the project in the future. After a while, I tried to pass an object to Android activity to give a possibility to edit some data in a form. Because you do not create activities directly, you cannot just pass them to a constructor or add a setter to it. It’s more complicated.

I started looking for an answer and I found a post on StackOverflow with few possible solutions.

Add a constructor

When the Activity is created a constructor without arguments is called. The solution is to add a constructor without parameters and put the data into it. The code would look similar to the below:

A problem with the code above is that if we want to add an another value we need to create a new class. If you have only a few permutations it may… oh no! It never makes sense!
Moreover, It breaks SOLID rules.

A static variable

Another solution I found was: change the variable to a static. Every time you want to create the activity you change the variable and create the activity. It will work but it may cause problems in the future. First of all, it’s harder to test with static variables. One of the StackOverflow’s users wrote:

Static variables represent global state. That’s hard to reason about and hard to test: if I create a new instance of an object, I can reason about its new state within tests. If I use code which is using static variables, it could be in any state – and anything could be modifying it.

I could go on for quite a while, but the bigger concept to think about is that the tighter the scope of something, the easier it is to reason about. We’re good at thinking about small things, but it’s hard to reason about the state of a million line system if there’s no modularity. This applies to all sorts of things, by the way – not just static variables.

Link to the post.

Use Intent.putExtra method

Using android.content.Intent.putExtra method seems to be the only right answer but everything can be complicated by someone.

Simple example

In the beginning, let’s take a look on a simple example. If you want to put a simple variable like String, Integer and so on, then you have to do 2 steps:

1. Put the variable to an Intent object

2. Fetch it in the activity:

and that’s all! Things make more complicated if you want to put other class, not a primitive type.

The problem with the android.content.Intent.putExtra method is that it takes as the second argument only basic types like int, string, short and so on. Howeover, there is a version with Parcelable interface as the second argument.

The parcel is an additional layer between the place you create the activity and the activity itself. Because you cannot pass any class to the putExtra method I found two ways to solve the problem.

Intent with serialization

Yeah, if you serialize an object to a string and deserialize it in the activity – it will work!

I would not trust this solution because it looks like a hack to me. The second reason is that serialization may be insecure. Moreover, it requires additional library and in some cases it may couse performance issues.

Intent with Parcels

The class you want to put into activity has to implement android.os.Parcelable interface. It’s described in the docs, of course 🙂

Let’s implement the interface and implement methods from the interface.

I created two constructors. The first one is a standard constructor we use every day. The second one will be used in the creator we will write in the next step. The describeContents method returns a flag – “describe the kinds of special objects contained in this Parcelable instance’s marshaled representation” (the source). We created a simple object so it always returns 0.
In the writeToParcel method we write all data we want to the parcel 🙂

In the another step we have to write a Creator. The creator’s responsibility is getting data from the parcel and creating a new object from it. The simplest creator I can imagine you can find below

It takes the parcel and calls BestClass constructor with it and returns it.

Summary

There is always more than 1 way to solve a problem. Not all you can find in google and SO are good. Some of them may be useful in some cases but you should be careful.

About author

Hi! I am Bartek. I'm a PHP developer but I other languages are not scary to me. My hobby is security and I try to learn as much as it's possible how to not be hacked. I like to know how things work. After hours I like playing Dota2 :)