How to switch between Activities in Android


When making your Android apps you may get to the point where you need to create multiple Activities for different screens within your app. When you have multiple Activities you will need a way to transition between them in a way that doesn’t break the back button functionality.

In order to switch between Activities in Android you will need to follow these steps:

  1. Create the Activities
  2. Add the Activities to the app’s Manifest
  3. Create an Intent referencing the Activity class you want to switch to
  4. Call the startActivity(Intent) method to switch to the Activity
  5. Create a back button on the new Activity and call the finish() method on an Activity when the back button is pressed

I have created sample code below also available on GitHub showing how to transition between Activities.

A video version of this tutorial is available on YouTube which I have also embedded below.

Transitioning Between Activities in Android

Create Two Activities

We will start by creating two Activities, FirstActivity and SecondActivity.

The FirstActivity will contain a ConstraintLayout with a TextView label showing “First Activity” and a Button with the text “Go to Second Activity”.

User Interface of the First Activity

The SecondActivity will contain a ConstraintLayout with a TextView label showing “Second Activity” and a Button with the text “Back to First Activity”.

Add Both Activities to the App Manifest

Ensure there is an activity entry for both Activities in the application section of the manifest file located at app/src/main/AndroidManifest.xml

Create the Intent and Start the Activity

Making the following changes to the FirstActivity class.

Add a new method called “switchActivities()”, inside this method we will create an Intent passing a parameters of the current activity and the new activity class. Then use the startActivity(Intent) method to passing the Intent that was just created.

Inside the onCreate method, add an OnClickListener to the Button used for switching the new Activity. When selected call the switchActivities() method.

Using the finish() method to preserve the back button functionality

Making the following changes to the SecondActivity class.

Inside the onCreate method, add an OnClickListener to the Button used for going back to the previous Activity. When selected call the finish() method will will remove the SecondActivity and take you back to the FirstActivity.

User Interface of the Second Activity

How can I pass data between Activities?

When switching between Activities you may need to pass data from the source Activity to the destination Activity.

This is possible by providing data to the Intent used to transition between Activities through the use of the putExtra(…) method.

Using the example above to pass data from the FirstActivity to the SecondActivity, use the putExtra(…) method inside the method for switching between Activities, to pass a message that includes a reference to the origin Activity.

Inside the SecondActivity class, add a TextView to display the result and inside the onCreate(…) method, call the getIntent() method and the getStringExtra(…) method on the Intent to retrieve the message and populate it into the result TextView.

What is an Intent in Android?

Intents are used in Android to request an action from other app components such as Activities, Services, Broadcast Receivers or Content Providers.

The main use cases for Intents are:

  1. Starting an activity
  2. Starting a service
  3. Delivering a broadcast

Intents can be Explicit or Implicit. Explicit intents explicit specify the application that will perform the action on the intent. Implicit are kept generic and request a general action to be performed which could be satisfied by a number of different apps.

Intent Filters are defined in the app’s manifest file to advise which actions from a Implicit Intent that it can handle.

Recent Posts