Android 14 API 34. No Activity found to handle Intent

668 views Asked by At

I had a working code. It is working fine till Android 13 but from Android 14 it is not working.

Problem: application needs to start a new activity-for-result. On launching new Activity, code is throwing exception.

Code:

ActivityResultLauncher<Intent> myNewActivity = myActivity.registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult()
    , result -> myActivityResult(getJotterReqNo(), result.getResultCode(), result.getData())
);

myNewActivity.launch(new Intent(getMyActivity().ACTIVITY_TO_LAUNCH));

Exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=app.my.application.MyActivity }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2239)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1878)
at android.app.Activity.startActivityForResult(Activity.java:5589)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:780)
at androidx.core.app.ActivityCompat$Api16Impl.startActivityForResult(ActivityCompat.java:854)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:245)
at androidx.activity.ComponentActivity$1.onLaunch(ComponentActivity.java:239)
at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:173)
at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:47)
at app.my.application.fragments.applicationsList.MainActivity.onListItemClicked(MainActivity.java:241)
at app.my.application.fragments.applicationsList.MainActivity$$ExternalSyntheticLambda5.onItemClick(Unknown Source:6)
1

There are 1 answers

0
Nishant Agarwal On

As Paul T mentioned, there has been a security change in Android that restricts implicit intents for apps targeting Android 14+.

  • Implicit intents are only delivered to exported components. Apps must either use an explicit intent to deliver to unexported components, or mark the component as exported.
  • If an app creates a mutable pending intent with an intent that doesn't specify a component or package, the system now throws an exception.

These changes prevent malicious apps from intercepting implicit intents that are intended for use by an app's internal components.

Option 1 :

<activity
    android:name=".AppActivity"
    android:exported="true">

Option 2 :

<activity
android:name=".AppActivity"
android:exported="false">
<intent-filter>
    <action android:name="com.example.action.APP_ACTION" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
// This makes the intent explicit.
Intent explicitIntent =
        new Intent("com.example.action.APP_ACTION")
explicitIntent.setPackage(context.getPackageName());
context.startActivity(explicitIntent);

You can read more here,

https://developer.android.com/about/versions/14/behavior-changes-14#security