Android : Call activity of another application

I have two Android applications,suppose they are "A" and "B", "A" has five activities and I want to call its specific activity from button click event of "B". I tested this way of calling one application from another:

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");
startActivity(LaunchIntent);

"com.testapp.ws" is the package name of "A".

This runs "A" from its first activity again not from the specific activity. How can I call A's specified activity?


Solution 1:

Grant,

The issue here is clearly a misunderstanding of the Android Application Model. Commonsware is absolutely correct about how to solve this problem. However, without understanding Android fundamentals, I can see why you are having difficulty applying it. So, a quick explanation:

Every action in Android begins with an Intent. This is particularly true for Activities. Every Activity has an Intent. To make the interface easy for the developers, you may respond to an Intent from the OS, OR you may create an Intent from the Activities class to use. In general, it is best practice to do the first option.

Responding to an Intent

When picking an Intent to respond to, you may literally respond to any Intent. This is called an Action. If I created an Intent called "FOO", the Bar Activity could pick it up and respond. We have conventions, however, and the primary of those is to prepend your package name to any Intent you make. For example "com.company.package.FOO". Simply put, this is so that we avoid collisions with other apps.

Every Activity may respond to different events. This is defined in the AndroidManifest.xml.

<activity android:name="Activity3" ... >
    <intent-filter>
      <action android:name="com.company.package.FOO"/>
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Above, we also set the category to DEFAULT, so that unless the user changes it, we'll be the only app that responds to our custom Intent. The way that we then call the Intent is by using the SAME NAME that we created (i.e. "com.company.package.FOO")

startActivity(new Intent("com.company.package.FOO"));

That's how it works! You would simply change the above "com.company.package.FOO" to your package name (defined by your application) and something meaningful. An example is "com.testapp.ws.SWAT_FLIES".

Why your code doesn't work

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.ws");

The above code looks for a specific KIND of Intent action. Remember when you made the AndroidManifest and the first Activity you put:

 <action android:name="android.intent.action.MAIN">
 <category android:name="android.intent.category.LAUNCHER">

Well... getLaunchIntentForPackage() only gets the Intent for that first Activity. That's WHY we make a custom Intent... First, because we don't really want it to be our 3rd Activity to be our start up... And second, becuase the OS will tell us only the startup Activity. We have to tell it with our OWN action (i.e. "com.testapp.ws.SWAT_FLIES")

Hope this helps,

FuzzicalLogic

Solution 2:

Step #1: Add an <intent-filter> to the third activity with a custom action:

<intent-filter>
  <action android:name="com.testapp.ws.SOMETHING_USEFUL"/>
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Step #2: Start that activity using an appropriate Intent:

startActivity(new Intent("com.testapp.ws.SOMETHING_USEFUL"));

Solution 3:

There are cases where you may not be using two applications you specifically have editing capabilities for or you may not want to make custom intents, so in that case there is an alternative (with better error checking for availability):

Intent intent = new Intent();
intent.setClassName("PACKAGE_NAME", "PACKAGE_NAME.TARGET_ACTIVITY");
if (isCallable(context, intent)) {
    // Attach any extras, start or start with callback
} else {
    // Respond to the application or activity not being available
}

Somewhere in the main class or in a subclass that handles general methods:

public static boolean isCallable(Activity activity, Intent intent) {
    List<ResolveInfo> list = activity.getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}