Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 616
Grant Avatar asked Jun 09 '12 11:06

Grant


People also ask

How do I call an activity from another app?

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 do I communicate between two apps on android?

Android inter-process communication At the simplest level, there are two different ways for apps to interact on Android: via intents, passing data from one application to another; and through services, where one application provides functionality for others to use.

Can an android app launch another app?

Using intents even allows your app to start an activity that is contained in a separate app. An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as "capture a photo").


2 Answers

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

like image 184
Fuzzical Logic Avatar answered Oct 02 '22 20:10

Fuzzical Logic


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"));
like image 23
CommonsWare Avatar answered Oct 02 '22 19:10

CommonsWare