Explain Codes LogoExplain Codes Logo

How to launch an Activity from another Application in Android

java
android-development
activity-launch
intent
Alex KataevbyAlex KataevΒ·Sep 24, 2024
⚑TLDR

In order to launch an Activity from another app, you need to create an Intent and set its ComponentName to the package and Activity class name of the external app:

Intent launchIntent = new Intent(); launchIntent.setComponent(new ComponentName("com.otherapp.package", "com.otherapp.package.OtherActivity")); startActivity(launchIntent);

Remember, the exported attribute of the target Activity should be set to true in the external app's AndroidManifest.xml. Alternatively, if you are launching the Activity outside an Activity context, use intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).

To launch the main Activity of the other app, use the getLaunchIntentForPackage method:

PackageManager pm = getPackageManager(); Intent launchIntent = pm.getLaunchIntentForPackage("com.otherapp.package"); if (launchIntent != null) { startActivity(launchIntent); // We can party now πŸŽ‰ }

Best practices for app-to-app interaction

When initiating activity launch from another application, a few best practices would ensure smooth operation:

  • Before invoking startActivity, check that launchIntent isn't null to avoid crashes.
  • Implement a try-catch when using getPackageManager() to handle instances where the package isn't found (NameNotFoundException).
  • Should the desired app be absent from the device, you could consider redirecting the user to the Play Store for app installation. This can be achieved using Intent.ACTION_VIEW and a specific app market URI.

Handling varied scenarios

Launching activities from another application may vary significantly based on different situations. Here are a few common cases:

When the package may not be installed:

try { startActivity(getPackageManager().getLaunchIntentForPackage("com.otherapp.package")); } catch (NullPointerException e) { Intent goToMarket = new Intent(Intent.ACTION_VIEW) // At least we tried 🀷 .setData(Uri.parse("market://details?id=com.otherapp.package")); startActivity(goToMarket); // This time, let's shop πŸ›οΈ }

To start a specific Activity within an app:

try { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.otherapp.package", "com.otherapp.package.SpecificActivity")); startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(context, "Sorry, this app hasn't RSVP'd πŸ™ƒ", Toast.LENGTH_SHORT).show(); }

To check if an app is enabled before launching:

PackageManager pm = getPackageManager(); try { if (pm.getApplicationInfo("com.otherapp.package", 0).enabled) { // Heeeere we go! } } catch (PackageManager.NameNotFoundException e) { // Nope, not today pal }

Enhancing reusability and compatibility

In the spirit of DRY code, consider embodying all these checks within a re-usable function. Here is an example:

public boolean launchExternalApp(String packageName) { PackageManager pm = getPackageManager(); Intent launchIntent = pm.getLaunchIntentForPackage(packageName); if (launchIntent == null) { return false; // Couldn't find the treasure map πŸ—ΊοΈ } launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { startActivity(launchIntent); return true; } catch (ActivityNotFoundException e) { return false; // X didn't mark the spot 😞 } }

Accessing apps through unique identifiers

In Android, every app is identified by its unique package name. This means we can pinpoint the exact app we're looking for by using the package name.

Common pitfalls and prevention techniques

Adhering to best practices when launching an app from another will prevent errors:

  • Use getLaunchIntentForPackage to get the main activity, or define the component name for a specific activity.
  • Include null checks and exception handling to avoid crashes.
  • Seek the help of intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) when you want to launch an activity from a non-activity context.
  • If the target app isn't installed, consider redirecting the user to the Play Store.