Android – Start Another Activity “Within the Same Application”

android-start-activity-same-app

You can start another activity within the same application by calling startActivity(), passing it an Intent that describes the activity “class name” you want to start. Alternatively, if the activity to be started has an action name defined within <intent-filter> in the AndroidManifest.xml we can use to call the activity too.

Objectives:

  • How to call “start” another activity within the same application?
  • How to prepare the Intent object using class name or action name?

Environment & Tools:

  • Android Developer Tools (ADT) (or Eclipse + ADT plugin)
  • AVD Nexus S Android 4.3 “emulator”
  • Min SDK 8

( 1 ) Create Android Application

  • File >> New >> Android Application
  • Enter App name: android-start-activity
  • Pakcage: com.hmkcode.android
  • Keep other defualt selections, go Next  till you reach Finish

( 2 ) Two Layouts for Two Activities

  • Define the layout for the first “main” activity and the second “another” activity to be started.
  • 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnStartAnotherActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="26dp"
        android:layout_centerHorizontal="true"
        android:text="Start Another Activity" />

</RelativeLayout>
  • res/layout/activity_another.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/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Another Activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

( 3 ) Two Activity Classes

  • src/com/hmkcode/android/ActivityMain.java
  • MainActivity.java is a simple activity class implementing OnClickListener to handle button click event.
  • When user click on the button we call onClick
  • Within the onClick method we create the intent object and pass it to startActivity(intent) method
package com.hmkcode.android;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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 btnStartAnotherActivity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnStartAnotherActivity = (Button) findViewById(R.id.btnStartAnotherActivity);

		btnStartAnotherActivity.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {

		Intent inent = new Intent(this, AnotherActivity.class);

		// calling an activity using <intent-filter> action name 
		// 	Intent inent = new Intent("com.hmkcode.android.ANOTHER_ACTIVITY");

		startActivity(inent);
	}

}
  • src/com/hmkcode/android/AnotherActivity.java
  • AnotherActivity.java is the activity we want to start. It has one text view saying “Another Activity”.
package com.hmkcode.android;

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

public class AnotherActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_another);
	}
}

( 4 ) Define Activities in the Manifest XML File

  • All activity should be defined with in the manifest.xml file
  • AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hmkcode.android"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.hmkcode.android.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
			android:name="com.hmkcode.android.AnotherActivity"
			android:label="@string/app_name" >
			<intent-filter>
				<action android:name="com.hmkcode.android.ANOTHER_ACTIVITY" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>
    </application>

</manifest>

Source Code @ GitHub