How To Switch Activities in Android

In this tutorial we will develop an application to show you how to move from one activity to another in an Android application.

the launch activity: a button to trigger the startactivity method and text

Main activity screen of application

The picture above shows you the first screen that is seen when the application is launched, it is composed of a button used to go to the other activity, which we will call “SecondActivity”. The second activity, shown below, will have its own button, this button will be used to go back to the previous (first) activity although you could also use the phone’s back button.

the second activity: a button to go back to the first activity and some text

The application’s second activity screen

The Start Activity Method

The startActivity method is used to move from one activity to another, it takes a parameter of type Intent. The Intent object requires a context and class parameters, the context parameter is simply the object of the activity that will trigger the startActivity method, and the class is the the activity you want to go to.

This is how you use the startActivity method:

Intent myIntent=new Intent(this,SecondActivity.class);
startActivity(myIntent);

An Activity’s Files:

Before we can tell Android to start an activity we have to add some files as well as modify others.

What we need to create is a java and layout xml file for each new activity,the java file is the activity’s functionality code, the xml file is the layout file. Eclipse already created a java and layout file for the main activity when you created the project.

Both of our activities have a button an onclick listener for that button, when the button is clicked the startActivity method with its Intent is called.

The full code for the application is given below.

MainActivity.java

package com.example.activitychangeexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

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

SecondActivity.java

package com.example.activitychangeexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondActivity extends Activity implements OnClickListener{

	Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

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

Manifest File

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitychangeexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>

</manifest>

res/layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="My Main Activity"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginBottom="16dp"
        android:text="Go to Another Activity" />

</RelativeLayout>

Related posts: