Moving between Activities with Intents— Android #8

Here’s a quick guide on how to let the user move between screens in your app. We’ll:
- add a new Activity
- add a Button that when pressed goes to that new Activity, using an Intent
- send information between the Activities using an Intent Extra
This is basic stuff, but is a core part of apps so deserving of its own post!
Let’s go!
Start a new project in Android Studio using an Empty Activity. You should then have one MainActivity.java file and one activity_main.xml file.
Let’s add another activity.
Right-click on app in the lefthand panel and select New → Activity → Empty Activity:

Give it a name — here I just use “SecondActivity”:

Hit Finish and you should see two new files created — a new Java file, SecondActivity.java and new layout XML file, activity_second.xml:

To allow the user to move from the MainActivity to the SecondActivity, we’re going to add a Button and a method that will be ran when that button is tapped.
To add the button, go to activity_main.xml and add a Button (below I first add a vertical LinearLayout and change the default “Hello World” TextView to “First Screen”:

which looks like:

To associate a method with this Button, we use the Button’s onClick property. Here I call it “onButtonClick” — the name of method we haven’t written yet:

or if you check the Text tab, you can see the XML code instead:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.whatever.twoscreens.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="18dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Screen"
android:textSize="24sp"
android:textAlignment="center" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="onButtonClick" />
</LinearLayout>
</RelativeLayout>
Right, open the MainActivity java file now and we’ll create the onButtonClick method:

Here’s the code for onButtonClick:
public void onButtonClick(View v){
Intent myIntent = new Intent(getBaseContext(), SecondActivity.class);
startActivity(myIntent);
}
What’s going on?
public void onButtonClick(View v){
is declaring the method. The void means that there’s no value “returned” from the method.
Intent myIntent = new Intent(getBaseContext(), SecondActivity.class);
Here we create a variable called myIntent which is an instance of the Intent Class, which we use to move between Activities. The important bit is:
SecondActivity.class
This is where we specify which Activity the Intent goes to. If we had called our Activity RecipeActivity, then that line would be instead:
Intent myIntent = new Intent(getBaseContext(), RecipeActivity.class);
Then:
startActivity(myIntent);
actually starts a new Activity, using the myIntent Intent variable.
Let’s try it out! Run the app and hit the button, it should go from the first screen to the second (blank) one.
So now you can add a new Activity and allow the user to go to it, using a Button which using an Intent.
Sending information between Activities.
Let’s say we want to pass over a value to the second Activity — one way is to use an Intent Extra. Add this line to your method:
myIntent.putExtra("value1", "Barcelona");
placed like so:

This creates an “extra” piece of data that has two parts — a Key and a Value. The Key is the identifier / name for the Value. So in our example, the Key is “value1” and the actual Value is “Barcelona”.
That’s one side of the story — sending a value. We now need to add code to the SecondActivity java file to receive the value. First, let’s add a TextView to the Second Activity — we’ll use this to display the information (“Barcelona”) that we’re sending from the first Activity:

You can see I’ve added a vertical LinearLayout and two TextViews — one to just say “Second Screen” and one to display our sent information. I give this on an ID of “textID”:

We use the ID to get a reference to the TextView in the java file. Go to that java file now — SecondActivity.java — and add this code to the onCreate method:
String savedExtra = getIntent().getStringExtra("value1");
TextView myText = (TextView) findViewById(R.id.textID);
myText.setText(savedExtra);
placed like:

The
String savedExtra = getIntent().getStringExtra("value1");
creates a new String variable called “savedExtra” and we store in it the Value of the Intent Extra with the Key “value1”. Note that it uses:
getStringExtra
This is because the value we sent is a String i.e. text. If it was a number, we’d use getIntExtra e.g.
myIntent.putExtra("value2", 200);
to send the number and
int savedExtraNumber = getIntent().getIntExtra("value2",0);
to retrieve it. Note the o at the end is a default value if the number sent is empty.
Ok, back to our code. This gets a reference to our TextView variable which has an ID of “textID”:
TextView myText = (TextView) findViewById(R.id.textID);
and
myText.setText(savedExtra);
sets that TextView to show the received information. Run it and you should see:

Boom. Any questions or thoughts, just comment below!
If you’ve any thoughts or comments, let me know below or you can get me on Twitter or LinkedIn. Thanks, Andy