How to Switch Between Activities in Android and Using Intents

Generally the complex Android Applications involve more than one screen and frequent switching between those screens. You also need to pass the data from one screen to another. In this tutorial we will learn how to switch between multiple activities in Android and how to pass data between them using intents. First of all I will tell you about the basic functions needed to create various activities and intents. After that we will create a test project to demonstrate the feature.

First Activity
First Activity

Second Activity
Second Activity

Download Code

How to start a new Activity

To start the new activity, Android has a prebuilt function startActivity. startActivity takes two parameters: the current application context and the new Activity. We will start the new activity on button click as:

Button go = (Button) findViewById(R.id.go);
go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Create the new intent
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
//Start the new activity
startActivity(intent);
}
});

Creating SecondActivity.java

Initially the SecondActivity.java will just contain the necessary code to show the content of the layout.

public class SecondActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
}
}

Declaring Second Activity in AndroidManifest file

We will also need to add the new activity class in the project’s AndroidManifest file. You will receive an error unless you add this activity in the AndroidManifest file. You can add it just below the first activity declaration as:

<activity 
android:name=".SecondActivity"
android:label="@string/app_name"/>

Pass data through Intents

Now we know how to start a new activity using intents, we need to pass some data from our first activity to the second. This is done using the putExtra function of the intent. putExtra passes the data in the form of key-value pair as:

intent.putExtra("MyEmail", email.getText().toString());

Receive data from Intents

Receiving data from intents is as simple as adding data to intents. The getIntent() function will return the data set in the intent as:

Intent intent = getIntent();
String name = intent.getStringExtra("MyName");

Creating Test Project

Now we will implement the activity switching in a test project. Create a new android project with project name ‘Actswitch’. For the first activity I have created FirstActivity.java. This file is responsible for creating the new activity and passing data to it.

Project Structure

Project Structure
Project Structure

Here is the code for FirstActivity.java

package com.example.actswitch;

import android.app.Activity;

public class FirstActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

Button go = (Button) findViewById(R.id.go);
go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Get the name text field
TextView name = (TextView) findViewById(R.id.name);
//Get the email text field
TextView email = (TextView) findViewById(R.id.email);

//Create the new intent
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);

//Set the name in intent
intent.putExtra("MyName", name.getText().toString());
//Set the email in intent
intent.putExtra("MyEmail", email.getText().toString());

//Start the new activity
startActivity(intent);
}
});
}

}

Layout for first activity activity_first.xml

Here is the layout file used in first activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#80000000" >

<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:hint="@string/name_hint"
/>

<EditText
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:hint="@string/email_hint"
/>

<Button
android:id="@+id/go"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/btn_text"
/>

</LinearLayout>

Creating SecondActivity.java

package com.example.actswitch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);

//Get the textview to show result
TextView result = (TextView) findViewById(R.id.result);

//Get the data from intent
Intent intent = getIntent();

//Create the result string
String data = "Welcome to Androidn";
data += "Name: " + intent.getStringExtra("MyName") + "n";
data += "Email: " + intent.getStringExtra("MyEmail") + "n";
//Set the string in the textview
result.setText(data);
}
}

Layout for second Activity activity_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Result..."
/>

</LinearLayout>

Adding second activity in AndroidMenifest File

<activity 
android:name=".SecondActivity"
android:label="@string/app_name"/>

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

2 thoughts on “How to Switch Between Activities in Android and Using Intents

Comments are closed.