How to start activity in another application?
Asked Answered
P

3

87

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!

Product answered 5/2, 2010 at 18:46 Comment(0)
P
173

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();
}
Product answered 5/2, 2010 at 20:9 Comment(15)
Glad to see this answer! However, in my case (Android 4.2), I got "Permission Denial: starting Intent ..." error. Any clue?Afghanistan
@Afghanistan add android:exported="true" to your activity propertyLinnet
@Afghanistan 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.Evidentiary
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?Bonhomie
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?Duralumin
Oops, sorry; I forgot part of my package name; that's why. Now it works fine.Duralumin
@Erhannis, what package name of MainActivityA has to do here? I am facing similar problem. Not sure, if I am missing something.Lixiviate
Well, see, my package name had 3 parts, so I had to do new ComponentName("com.exampleb.crossappobjecttestb", "com.exampleb.crossappobjecttestb.MainActivityB").Duralumin
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?Lixiviate
@Product And can you please tell me how can we stop an activity which is started this way, by adding component in intent.Reorder
@Product after adding exported = "true" it is still not crashing with same errorCorwin
When you set android:exported="true" it is good idea to set android:permission="YOUR_PERMISSION" too.Albanese
@Duralumin 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?Rigadoon
@Rigadoon Apparently I had forgotten part of a package name. It worked fine after I fixed it.Duralumin
GET your PACKAGE NAME from the MANIFEST File, NOT from the Class File.Kilocycle
B
17

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.

Blackamoor answered 4/1, 2014 at 23:17 Comment(3)
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");Kush
@TimAutin What if I need to launch a specific activity that belongs to an app I don't have control over?Permafrost
I never had to do that, so I don't know. Did you try this answer https://mcmap.net/q/237676/-how-to-start-activity-in-another-application ?Kush
M
1

Make sure the target activity you are trying to start is exportable. To check this, go to the AndroidManifest.xml file of the target application and check the activity's declaration has the below code:

android:exported="true"

One this is confirmed. Then you can use this code in the source application from where you want to start the target activity.

try {
      Intent().apply {
      component = ComponentName(
                "com.example.application",
                "com.example.application.ActivityName"
               )
      startActivity(this)
      }
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace(System.out)
    }
Mukden answered 24/7, 2023 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.