13

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?

Grant
  • 3,826
  • 16
  • 50
  • 77

4 Answers4

41

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

Fuzzical Logic
  • 12,736
  • 2
  • 28
  • 58
  • Thanks Mr.Fuzzical Logic for the detailed description. After referring your explanation I have implemented the app successfully. Thank you so much! – Grant Jun 09 '12 at 14:34
  • how this can be achieved if i want to send the data from one activity in one app to a service in another application.??? – Aada Mar 05 '13 at 10:55
  • Actually, you just need the Intent ACTION and send the data via setExtra before you you fire the startService(). You might have to construct the Intent first (i.e. Intent myIntent = new Intent("com.company.app.ACTION") – Fuzzical Logic Mar 05 '13 at 23:11
  • Is it possible to call the activity within a tabHost... I tried it as "firstTabSpec.setIndicator("Second Tab Name").setContent(new Intent("com.company.package.FOO"));" but i'm getting an "java.lang.SecurityException: Requesting code from com.company.package (with uid 10036) to be run in process com.example.test (with uid 10037)" – kAnNaN Mar 27 '13 at 06:59
  • Yes, it is, but it depends on several things. The most important of which is what you mean by calling it within a Tab. Do you mean that you want the Tab to use the Activity for its View or do you mean when you "click" an item it will open the Activity named. – Fuzzical Logic Apr 16 '13 at 18:13
  • i have created custom intent for my activity in app1 and I want to start the activity in app2 by calling startActivity(new Intent("com.company.package.APP1")); but intent not found – yeahman Mar 24 '18 at 21:23
26

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"));
CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • What I should add to here ?? "SOMETHING_USEFUL" in xml and Activity class ? Can you please explain more ? Thanks! – Grant Jun 09 '12 at 11:56
  • 1
    What about the `category`, is it necessary here? – Eng.Fouad Jun 09 '12 at 12:00
  • 1
    @Eng.Fouad: Ah, yes, you will need a `` element for the `DEFAULT` category in that `` -- my apologies. – CommonsWare Jun 09 '12 at 12:16
  • @Grant: You could literally use the code that I have listed here. If you wish to change `SOMETHING_USEFUL` to be something more useful, just do so in both places. – CommonsWare Jun 09 '12 at 12:17
  • @Grant add the `` to the activity into AndroidManifest.xml file. – Eng.Fouad Jun 09 '12 at 12:21
  • Do I need to replace "SOMETHING_USEFUL" with my activity class name ?? Please explain I'Mm new to programming! – Grant Jun 09 '12 at 12:51
  • Thanks in advance CommonsWare! I was able to implement this successfuly with the help of the Fuzzical Logic's explanation. Thanks again! :) – Grant Jun 09 '12 at 14:41
  • @CommonsWare I am now able to call the Activity, but I want to send the Bundle Values too, can i do this while calling any other Project's Activity ? – Gaurav Arora Oct 31 '14 at 06:27
  • @GauravArora: Include extras in the `Intent` you use with `statActivity()`. If you have additional concerns in this area, I suggest that you open a separate Stack Overflow question. – CommonsWare Oct 31 '14 at 06:36
  • Thanks @CommonsWare. Please check it out http://stackoverflow.com/questions/26668986/sending-bundle-with-explicit-intents – Gaurav Arora Oct 31 '14 at 06:51
  • are we suppose to declare the activity in the manifest of the calling app like mentioned here http://stackoverflow.com/a/9839254/1638739 – Muhammed Refaat Jun 02 '16 at 08:39
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;
}
Abandoned Cart
  • 3,230
  • 29
  • 32
1

here is the code to open an app(ex whatsapp) from another app

public class MainActivity extends Activity{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button bClock = (Button) findViewById(R.id.button1);
    bClock.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Intent i = new Intent(Intent.ACTION_MAIN);
    PackageManager managerclock = getPackageManager();
    i = managerclock.getLaunchIntentForPackage("com.whatsapp");
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
    }
    });

} }

bhavithra
  • 155
  • 2
  • 7