Passing data between Activities

Communicating with Objects and instance variables outside its parent class has always been a headache to new Android developers who are not very comfortable with android concepts.

while working on a multiple activity project i decided to write on the means i employed to establish communication between Activities. i have written a detailed but simple explanation of the process here so read, understand,use, share, be inspired.

First thing to consider when passing data between activities is whether or not you want to make the data persistent. passing data to other activities whilst making them persistent can be achieved in various way including but not limited to shared preferences and use of database but we’ll not cover it in this lesson.

For this lesson we are only concerned with passing data between activities without making them persistent so we’ll look at the two simplest ways to achieve that which are by using Static methods and Intents

1. Using Static Methods

This process is fairly straight forward so am going to support the texts with a few lines of code for demo. So lets say i want to create a String variable in my MainActivity class and i have a SecondActivity class where i would also like to call that variable.

First create the desired string variable inside the MainActivity class and initialize it as you want, in my case am going to initialize it to kennny.

Here i have created a static string variable (data) and set it’s value to “kennny”. also note that it was explicitly declared so that other static methods within the class can access it.

Next i will define a public static method that will return the value of the data variable.

Here the getData Method returns the value of the ‘data’ variable. also notice that the method is a static method with a String return type.

At this point you can access the data variable in another class, you can go ahead and try working with other static methods but i will jump over to the next Activity and show you how to call the getData method from another activity.

In order to maintain uniformity, we need to define another String variable ‘data’ in the SecondActivity and set its value to read the value of the corresponding ‘data’ variable in the MainActivity.

Here we have successfully made the value of data in the MainActivity equal to the value of data in the SecondActivity and if the value changes in the MainActivity at some point, it equally updates in the SecondActivity.

That’s basically how to pass data between different Activities in a package using static methods and variables, however the data you can pass is not limited to Strings. You can Log the value of the data in the SecondActivity to see if it displays it’s value in the console.

If you are correct, and you used the same values i used, you should have this displayed in your console | data value Kennny.

2. Using Intents

Intents are the most generally accepted way of passing primitive data and Strings between Activities. They offer more flexibility and allows more values.

There are various ways of passing data with intents but we’ll cover just the two simplest ways to get you started while we treat the other complex ones in the next topic. first we’ll start with the Intent Direct Passing method.

Direct Method.

Here we will create the intent and use the intent putExtra methods to add data directly into the intent. this is how we create an Intent.

Here MainActivity is the current Activity and (.this) shows the current context while SecondActivity is the activity that we want to start and pass the data to thus, we are creating a new Intent that will have it’s keys and values defined in the MainActivity but will equally be passed to the SecondActivity.

Next we add data (keys and values ) into the intent with the putExtra methods

Note that this intent is what also starts the SecondActivity

Now we have created an Intent, added a key and a value into it and started the intent. lets head on over to the SecondActivity and see how to retrieve the data from the intent directly

First in the SecondActivity we have to create an intent that will get the data we’ve stored in the MainActivity.

String name; was explicitly declared

The first line of code creates an Intent that will get our data from the Intent we passed from the MainActivity class. then we set the string variable (name) to store the value carried by the intent from the MainActivity. Note both Intents has the same key (name) for the sake of clarity and uniformity.

So we have successfully passed the data from the MainActivity to the SecondActivity and i always advise to Log the value of the passed data to make sure it displays in the console.

Bundle Method

Android has a class called Bundle where we can store our data and it supports several data types like strings, chars, boolean, integer and so on.
Instead of using the Direct Intent as data container, we can store our data directly into the bundle and then save the bundle into the Intent.

This process is relatively the same as the Direct Method only that in this case the data is stored into the bundle instead of the intent instance.
In this case, in the MainActivity we have:

Here we created the Intent and also created the bundle to store the data then afterwards put the bundle inside the intent so the intent can carry it over to the SecondActivity.

Then in the SecondActivity, this is how we call the Intent

Here we first extracted the data from the bundle with the getBundleExtra Method and then store it in the String variable name. as usual we can always Log the value in the console.

And if you did it all right, you should get the value of the variable name displayed in your console.

Congratulations, now you understand how to pass data between Activities, next we’ll be building an app that uses Intents to pass data and share content. keep practicing, do your best and learn everyday.