Why can't Activity.startActivity() find my activity, on API 33 only?

410 Views Asked by At

I have a bug that only happens on API 33. Did something change? I mean, things definitely changed, but I made all the necessary updates. Did I miss one?

I have two apps: A and B. Let's call them com.example.a and com.example.b. They need to launch each other. As of API 33, when app A tries to launch app B, I get:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.b/com.example.b.DefaultLaunchActivity}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared <intent-filter>?

DefaultLaunchActivity is defined in app B's manifest like this:

<activity
    android:name="com.example.b.DefaultLaunchActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

I also added the following to app A's manifest (required as of API 31):

<queries>
    <package android:name="com.example.b" />
</queries>

Finally, app A gets app B's launch Intent like this:

val intent = application.packageManager.getLaunchIntentForPackage("com.example.b")?.apply {
    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
}

Adding Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER doesn't help. App B gets the same error when it tries to launch app A.

To be clear, the code above works on API 32 but not API 33. What changed?

6

There are 6 best solutions below

2
Gleichmut On

Yes, many things have changed.

You have to explicitly define in the manifest file packages of apps you are going to call, e.g.:

    <queries>
        <package android:name="com.google.android.apps.translate"/>
    </queries>

It was introduced in Android 11 (API 30). The observed behavior might be explained as it was introduced in API 30, but forced since API 33. Also others causes are possible. It will require more research to figure out a root cause.

Ref. Android docs: queries

Ref. Android docs: Behavior changes: Apps targeting Android 11

update I noticed you already use queries attribute, so an issue cause lays somewhere else

0
Dev4Life On

You have provided the package name of the destination app. Maybe, you can try adding these lines in the query tag.

<queries>
    <package android:name="com.example.b" />
    <intent>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME,android.intent.category.DEFAULT" />
    </intent>
</queries>
0
Laxmi kant On

In your AndroidManifest.xml snippet, you've declared activity with android:exported="true", which should make the activity accessible from other apps.

Here some things to check and consider:

Use Explicit Component

To be explicit, you can use the component name when starting the activity. This ensures that Android can find the activity directly by its name.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.b", 
"com.example.b.DefaultLaunchActivity"));
startActivity(intent);
0
ΓDΛ On

You can use intent as standard for this process. In addition to other answers you can use this technique.

val intent = Intent()
intent.component = ComponentName("com.example.otherapp", "com.example.otherapp.ActivityName")
startActivity(intent)

param 1 : OtherApp package name param 2 : activity you want to open

Other App

<activity android:name=".ActivityName" 
          android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
0
Jakub Kalinowski On

Did you try adding

<queries>
    <package android:name="com.example.a" />
</queries>

to AndroidManifest of app B?

0
Giedrius Kairys On

You need to add <category android:name="android.intent.category.DEFAULT" /> in the <intent-filter> for com.example.b.DefaultLaunchActivity for it to be able to receive implicit intents.:

https://developer.android.com/guide/topics/manifest/category-element https://developer.android.com/guide/topics/manifest/category-element

https://developer.android.com/guide/components/intents-filters#ExampleSend https://developer.android.com/guide/components/intents-filters#ExampleSend