How to pass a variable to Android activity


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 has 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:

class MyActivity extends Activity
    protected String myString;

class MySecondActivity extends MyActivity
        this.myString = "Super value";


A problem with the code above is that if we want to add 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 methodandroid.content.Intent.putExtra seems to be the only right answer but everything can be complicated by someone.

Simple example

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

  • Put the variable to an Intent object
Intent myIntent = new Intent(MyActivity.this, WalletEditActivity.class);
myIntent.putExtra("best_name", "Bartek");


  • Fetch it in the activity:
String bestName = (String)this.getIntent().getExtras().get("best_name");


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

The problem with the methodandroid.content.Intent.putExtra is that it takes as the second argument only basic types like int, string, short and so on. However, there is a version with interfaceParcelable as the second argument.

The parcel is an additional layer of the place you create the activity and the activity itself. Because you cannot pass any class to the methodputExtra 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!

String strEmp = new Gson().toJson(someBestClass);
Intent intent = new Intent(getBaseContext(), MyActivity.class);
intent.putExtra("best_class", strEmp);

// in the activity
Bundle bundle = getIntent().getExtras();
String empStr = bundle.getString("best_class");
Gson gson = new Gson();
Type type = new TypeToken<BestClass>() {
BestClass selectedEmp = gson.fromJson(empStr, type);

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 cause performance issues.

Intent with Parcels

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

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

import android.os.Parcel;
import android.os.Parcelable;

public class BestClass implements Parcelable
    private String bestName;

    public BestClass(String name) {
        this.bestName = name;

    protected BestClass(Parcel in) {
        bestName = in.readString();

    public int describeContents() {
        return 0;

    public void writeToParcel(Parcel dest, int flags) {

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 instanceParcelable’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 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

public static final Creator<BestClass> CREATOR = new Creator<BestClass>() {
    public BestClass createFromParcel(Parcel in) {
        return new BestClass(in);

    public BestClass[] newArray(int size) {
        return new BestClass[size];

It takes the parcel and calls the constructorBestClass with it and returns it.


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