How to switch from one activity to another in Android?

SHARE & COMMENT :

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 “First_activity.java” 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: my.app
  • 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="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="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">
</TextView>
<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">
</Button>
</LinearLayout>

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="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</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"
android:textSize="15sp"></TextView>
</RelativeLayout>

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”. First_activity.java is the launcher activity that gets created automatically when you create a new project. Create a new activity Second_activity.java.

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 First_activity.java is as below:


package my.app;
import android.app.Activity;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_first_activity);
navigate=(Button)findViewById(R.id.Button01);
navigate.setOnClickListener(new OnClickListener() {

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

Add the below code to the activity “Second_activity.java”.


package my.app;
import android.app.Activity;
import android.os.Bundle;

public class Second_activity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second_activity);
}
}

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 “Second_activity.java”.


<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="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
package="my.app" 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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Define Second_activity here -->
<activity android:name="Second_activity"></activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>

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.

switch2

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

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better