Android – Start Another Activity “of Another Application”

android-start-activity-another-app

You can start another activity of  another application by calling startActivity(), passing it an Intent that has the activity action name defined within <intent-filter> in the AndroidManifest.xml.

Objectives:

  • How to call “start” an activity of another application?
  • How to prepare the Intent object using action name of the activity we want to call?

Environment & Tools:

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

( 1 ) Create Tow Android Applications

First Application Name: App

  • File >> New >> Android Application
  • Enter Project Name: android-app
  • Pakcage: com.hmkcode.android
  • Keep other defualt selections, go Next  till you reach Finish

Second Application Name: Another App

  • File >> New >> Android Application
  • Enter Project Name: android-another-app
  • Pakcage: com.hmkcode.android.another.app
  • Keep other defualt selections, go Next  till you reach Finish

( 2 ) Create Two Layouts for Two Activities

  • First Application: App
  • Create a layout for the first app main activity.
  • The layout contains a button “Start Another App Activity”
  • android-app/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 App Activity" />

</RelativeLayout>
  • Second App: Another App
  • Create a layout for the second app “Another Activity”
  • The layout contains one text view saying “Another App Activity”
  • android-another-app/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 App Activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

( 3 ) Create Two Activity Classes

  • First App: App
  • android-app/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.
  • The string pass to the Intent “com.hmkcode.android.another.app.ANOTHER_ACTIVITY” is the intent action name of the activity located in the second App.
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) {

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

		startActivity(inent);
	}

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

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 AnotherActivity & Its Intent Filter in the Manifest XML File

  • First App: App
  • Keep the default “generated” AndroidManifest.xml without changes.
  • Second App: Another App
  • Here is an important step, we need to define the intent action name that will be used to call the activity com.hmkcode.android.another.app.ANOTHER_ACTIVITY
  • It is important to give it a “globally” unique name.
  • android-another-app/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hmkcode.android.another.app"
    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.another.app.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.another.app.AnotherActivity"
			android:label="@string/app_name" >
			<intent-filter>
				<action android:name="com.hmkcode.android.another.app.ANOTHER_ACTIVITY" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>
    </application>

</manifest>

3 thoughts on “Android – Start Another Activity “of Another Application”

  1. Julie Dixon

    This didn’t work for me. I got the following error:
    W/dalvikvm( 588): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    E/AndroidRuntime( 588): FATAL EXCEPTION: main
    E/AndroidRuntime( 588): android.content.ActivityNotFoundException: No Activity
    found to handle Intent { act=com.hmkcode.android.another.app.ANOTHER_ACTIVITY }
    E/AndroidRuntime( 588): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
    E/AndroidRuntime( 588): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
    E/AndroidRuntime( 588): at android.app.Activity.startActivityForResult(Activity.java:3351)
    E/AndroidRuntime( 588): at android.app.Activity.startActivityForResult(Activity.java:3312)
    E/AndroidRuntime( 588): at android.app.Activity.startActivity(Activity.java:
    3522)
    E/AndroidRuntime( 588): at android.app.Activity.startActivity(Activity.java:
    3490)
    E/AndroidRuntime( 588): at com.hmkcode.android.MainActivity.onClick(MainActivity.java:30)
    E/AndroidRuntime( 588): at android.view.View.performClick(View.java:4084)
    E/AndroidRuntime( 588): at android.view.View$PerformClick.run(View.java:16966)
    E/AndroidRuntime( 588): at android.os.Handler.handleCallback(Handler.java:615)
    E/AndroidRuntime( 588): at android.os.Handler.dispatchMessage(Handler.java:92)
    E/AndroidRuntime( 588): at android.os.Looper.loop(Looper.java:137)
    E/AndroidRuntime( 588): at android.app.ActivityThread.main(ActivityThread.java:4745)
    E/AndroidRuntime( 588): at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime( 588): at java.lang.reflect.Method.invoke(Method.java:511)
    E/AndroidRuntime( 588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    E/AndroidRuntime( 588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    E/AndroidRuntime( 588): at dalvik.system.NativeStart.main(Native Method)

    1. hani.hmk Post author

      Hi Julie,

      Did you create the second App. it seems the code is not detecting the “com.hmkcode.android.another.app.ANOTHER_ACTIVITY” action name.
      I will put the code on my GitHub soon

Comments are closed.