How to switch from one activity to another in Android?

This tutorial explains you about how to Navigate from one activity to another in Android. I Presume you have enough basic knowledge about how to create a sample application in android and How to add a new Activity to an already created sample app. Let us now discuss how we can make two activities talk to each other.

Step 1: Create a new application in Eclipse IDE

Create the Navigation_Android project with “” as the launcher activity. To do this Go to File->New->Project->Android->Android Project.

Fill up all the details in the window that opens up:

  • Project Name: Navigation_Android.
  • Application name: Navigation
  • package name:
  • Create Activity: First_activity
  • Min SDK version: 8

Click on “Finish” after entering all the above details in the window. For more details on how to create a sample application, please refer create a simple app.

Step 2: Create the layouts for your activities

Create the following layout xml files.

  • layout_first_activity.xml(for first activity “First_activity”)
  • layout_second_activity.xml(for second activity “Second_activity”)

To create a layout xml file Go to file->New->Android XML File. Then give a name to your layout file with .xml extension and click “Finish”. When you create a new project a main.xml layout file will get created automatically. You can edit the contents of it to suit your requirements or you can delete it and create your own layout file for the launcher activity. Here I have created  “layout_first_activity.xml” file for first activity(LAUNCHER).

The code of the first layout “layout_first_activity” is as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a href=""></a>"
android:orientation="vertical" android:layout_height="fill_parent"
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textStyle="bold" android:layout_gravity="center"
android:layout_marginTop="30dip" android:text="Hi you are in the first activity">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Click to Navigate"
android:layout_gravity="center" android:layout_marginTop="40dip">

The code of the second layout “layout_second_activity” is as below:

<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="<a href=""></a>">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Welcome to Second Activity"
android:layout_centerInParent="true" android:textStyle="bold"

If you have any doubts on how to create layout xml file, please refer create a layout xml file.

Step 3: Create the two Activities

Lets name our activities as “First_activity” and “Second_activity”. is the launcher activity that gets created automatically when you create a new project. Create a new activity

To do this right click on the project or the package under which you need to create your activity->New->class
Fill the required details and click “Finish”. If you have any doubts on how to create activities,please refer create a new activity.

If you want to start an activity from another activity you have to use startActivity(Intent)  method,Which takes a single argument, an Intent, which describes the Activity to be executed.

The code snippet that you need to add to your first activity to start the second one is as below.

Intent intent=new Intent(First_activity.this, Second_activity.class);

If you want to get any result back to the first activity when the second activity finishes then you need to start the second activity using the method startActivityForResult() . Once the second activity is closed you need to use onActivityResult() method to read the result returned from the second activity.

The code of the is as below:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class First_activity extends Activity {
/** Called when the activity is first created. */
Button navigate;
public void onCreate(Bundle savedInstanceState) {
navigate.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(First_activity.this, Second_activity.class);

Add the below code to the activity “”.

import android.os.Bundle;

public class Second_activity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

Now your activities are ready to Run.

Step 4: Defining Activities in AndroidManifest.xml:

Last but not the least you’ll have to add your activities to your AndroidManifest.xml file. Your first activity will be automatically defined. Add the below code by double clicking on your manifest file to define the second activity “”.

<activity android:name="Second_activity"></activity>

The complete code of AndroidManifest.xml is as below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="<a href=""></a>"
package="" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".First_activity" android:label="@string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!--Define Second_activity here -->
<activity android:name="Second_activity"></activity>
<uses-sdk android:minSdkVersion="8" />

Step 5: Run your Application:

To do this Right click on the project you need to run->Run As->Android Application. Now your application fires up and you’ll get to see your first activity on Emulator screen.

Now click on the button “Click to Navigate” to switch to next Activity.


This is how you can achieve Navigation between Activities in Android. If you have any queries please post it in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>