Search code examples
androiddeep-linkingandroid-12

[Android]context.startActvity(intent) throwing ActivityNotFoundException


My use case is to switch from App1 to App2 . In App1, I use the packageManager.getPackageInfo() API to check whether the App2 is present or not. When it returns true, I set the intent with intent.setPackage(App2) and trigger the context.startActivity(intent) API. But there are times when context.startActivity(intent) API throws ActivityNotFoundException: App2 is present in the device. The intent has been set in App2 and android:exported is also set to true.

This issue is happening from Android 12. Any inputs be would be highly appreciated!


Solution

  • There is an official documentation on how to prepare an Activity to be launched from outside of the app: https://developer.android.com/training/basics/intents/filters


    Furthermore there are multiple examples on how to launch an external activity, maybe check out these two examples (Source for the examples):

    1.

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
    startActivity(intent);
    

    2.

    Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
    if (launchIntent != null) { 
        startActivity(launchIntent);//null pointer check in case package name was not found
    }