26

From one of my apps, I'm trying to launch another. I want to use an explicit intent.

ComponentName cn = new ComponentName("com.myOtherApp", "OtherAppActivity");
Intent intent = new Intent();
intent.setComponent(cn);
context.startActivity(intent);

When I run that code, however, it asks if I've declared that activity in my manifest. However, when I put the following into the manifest, I get the same error:

<activity android:name="com.myOtherApp.OtherAppActivity">
</activity>

What am I doing wrong?

Thanks

6 Answers 6

25

Try something like this...

In the manifest for 'myOtherApp' use an intent filter for 'OtherAppActivity' with a company specific intent, example...

<activity
    android:name=".OtherAppActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.mycompany.DO_SOMETHING" />
    </intent-filter>
</activity>

Then, in the 'calling' app, use...

Intent intent = new Intent();
intent.setAction("com.mycompany.DO_SOMETHING");
context.startActivity(intent);
Sign up to request clarification or add additional context in comments.

3 Comments

Easy and hassle-free. I like this approach.
How can I navigate back to first app ...?
Note that the intent filter has the include <category android:name="android.intent.category.DEFAULT"/>
22

I had this problem and searched for hours looking for a solution. Finally found it: http://www.krvarma.com/2010/08/launching-external-applications-in-android. That link shows how to use the package manager to launch any application for which you have simply the package name:

PackageManager pm = this.getPackageManager();

try
{
  Intent it = pm.getLaunchIntentForPackage(sName);

  if (null != it)
    this.startActivity(it);
}

catch (ActivityNotFoundException e)
{
}

Comments

19

You need to specify the fully qualified class name in the second parameter of new ComponentName like this:

ComponentName cn = new ComponentName("com.myOtherApp", "com.myOtherApp.OtherAppActivity");

I think this is because the package name in the manifest and the activity name don't necessarily have to have the same package path, so the new ComponentName call doesn't infer the class name second parameter is prefixed by the package name first parameter.

3 Comments

Just tested. <code>new ComponentName("com.myOtherApp", ".OtherAppActivity");</code> doesn't wotk
@WuYongzheng Exactly my point; my answer is that the activity name needs to be a fully qualified path. So your example should be changed to <code>new ComponentName("com.myOtherApp", "com.myOtherApp.OtherAppActivity");</code>
your answer and Jon's answer both work. My app got stuck and I realized the incomplete class name bug, so I think it's good to share. The Intent.toString() showing incomplete class name is a bit misleading.
2

As of API23, you could use the method ComponentName.createRelative(String pkg, String cls) and do:

ComponentName cn = new ComponentName(ComponentName.createRelative("com.myOtherApp", ".OtherAppActivity"));
Intent intent = new Intent();
intent.setComponent(cn);
context.startActivity(intent);

This way you can create a ComponentName object using a relative class path. Mind the dot in the start of the class path. It is necessary to indicate that the method should treat the second argument as a relative path. Just as @Sogger mentioned, the ComponentName constructor constraints the class parameter to be an absolute path.

Note also that by this manner, you are using explicit intents and you don't have to insert any additional intent filters to the destination activity.

Comments

0

In addition to @Sogger answer thing to remember is if you receiver class is com.myOtherApp.receiver.OtherAppActivity and package mentioned in AndroidManifest is com.myOtherApp your code will be

ComponentName cn = new ComponentName("com.myOtherApp", "com.myOtherApp.receiver.OtherAppActivity");

Comments

-2

Create the intent as action.Main and add the launcher category to it:

Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.