80

I have application A defined as below:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.example.MyExampleActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Now in application B, how can I write the code to start the activity in application A? Thanks!

naXa
  • 26,677
  • 15
  • 154
  • 213
user256239
  • 16,407
  • 24
  • 74
  • 89

2 Answers2

155

If you guys are facing "Permission Denial: starting Intent..." error or if the app is getting crash without any reason during launching the app - Then use this single line code in Manifest

android:exported="true"

Please be careful with finish(); , if you missed out it the app getting frozen. if its mentioned the app would be a smooth launcher.

finish();

The other solution only works for two activities that are in the same application. In my case, application B doesn't know class com.example.MyExampleActivity.class in the code, so compile will fail.

I searched on the web and found something like this below, and it works well.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);

You can also use the setClassName method:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.hotfoot.rapid.adani.wheeler.android", "com.hotfoot.rapid.adani.wheeler.android.view.activities.MainActivity");
startActivity(intent);
finish();

You can also pass the values from one app to another app :

Intent launchIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.hotfoot.rapid.adani.wheeler.android.LoginActivity");
if (launchIntent != null) {
    launchIntent.putExtra("AppID", "MY-CHILD-APP1");
    launchIntent.putExtra("UserID", "MY-APP");
    launchIntent.putExtra("Password", "MY-PASSWORD");
    startActivity(launchIntent);
    finish();
} else {
    Toast.makeText(getApplicationContext(), " launch Intent not available", Toast.LENGTH_SHORT).show();
}
Prags
  • 2,277
  • 2
  • 18
  • 32
user256239
  • 16,407
  • 24
  • 74
  • 89
  • 1
    Glad to see this answer! However, in my case (Android 4.2), I got "Permission Denial: starting Intent ..." error. Any clue? – JackWM Feb 22 '13 at 16:22
  • 12
    @JackWM add android:exported="true" to your activity property – Evan_HZY Jan 21 '14 at 21:01
  • 3
    @JackWM it will also work if the activity you're trying to launch has an intent filter. This is because the default value for the `android:exported` XML attribute is `true` when an intent filter is present. – Trebor Rude Jul 11 '14 at 16:19
  • Now that when you have started the activity of 2nd application from the 1st application,any clue of how to get programmatically in the 2nd application that which activity(or application) started that activity?Answer will be 1st application of course but how to get it in your 2nd application programmatically? – Sash_KP Sep 04 '14 at 16:03
  • 3
    Hmm, not working for me. I have two apps, each with one activity: `com.examplea.MainActivityA` and `com.exampleb.MainActivityB`. From MainActivityA I run your code snippet, with strings "com.exampleb" and "com.exampleb.MainActivityB". However, I just get `android.content.ActivityNotFoundException: Unable to find explicit activity class {com.exampleb/com.exampleb.MainActivityB}; have you declared this activity in your AndroidManifest.xml?` – Erhannis Oct 18 '14 at 02:41
  • Oops, sorry; I forgot part of my package name; that's why. Now it works fine. – Erhannis Oct 18 '14 at 02:47
  • @Erhannis, what package name of MainActivityA has to do here? I am facing similar problem. Not sure, if I am missing something. – Kaps Oct 24 '14 at 06:28
  • Well, see, my package name had *3* parts, so I had to do `new ComponentName("com.exampleb.crossappobjecttestb", "com.exampleb.crossappobjecttestb.MainActivityB")`. – Erhannis Oct 24 '14 at 20:19
  • okay, this is precisely what I am doing, but I still get similar error. One point to add, the activityB(of appB) I am trying to launch (from appA) has custom intent filter(NOT, action MAIN and catagory LAUNCHER) defined which I want to launch by this approach, not by adding appropriate intent filter to my intent. I hope this should still work. What do you think? – Kaps Oct 25 '14 at 04:48
  • @user256239 And can you please tell me how can we stop an activity which is started this way, by adding component in intent. – Dhrumil Patel Mar 22 '16 at 12:33
  • @user256239 after adding exported = "true" it is still not crashing with same error – Roon13 Aug 02 '17 at 12:39
  • When you set android:exported="true" it is good idea to set android:permission="YOUR_PERMISSION" too. – Nuwisam Sep 15 '17 at 10:38
  • @Erhannis are you able to launch properly? for me getting same exception android.content.ActivityNotFoundException: Unable to find explicit activity class {com.exampleb/com.exampleb.MainActivityB}; have you declared this activity in your AndroidManifest.xml? – sandeepmaaram Apr 19 '18 at 11:46
  • @sandeepmaaram Apparently I had forgotten part of a package name. It worked fine after I fixed it. – Erhannis Apr 19 '18 at 14:25
  • GET your PACKAGE NAME from the MANIFEST File, NOT from the Class File. – M. Usman Khan Feb 06 '19 at 15:45
15

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows:

Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(CALC_PACKAGE_NAME);
startActivity(LaunchIntent);

Hope it helps.

azelez
  • 2,421
  • 2
  • 25
  • 26
  • 7
    You don't need both apps to have the same signature. You can for instance launch Google Maps with this: Intent i = getPackageManager().getLaunchIntentForPackage("com.google.android.apps.maps"); – Tim Autin Apr 28 '16 at 11:20
  • 1
    @TimAutin What if I need to launch a specific activity that belongs to an app I don't have control over? – Epicality Jan 19 '18 at 22:37
  • 1
    I never had to do that, so I don't know. Did you try this answer https://stackoverflow.com/a/2210073/1356106 ? – Tim Autin Jan 22 '18 at 11:30