Search code examples
androidandroid-intentandroid-manifestintentfilter

starting Intent(Context(), X.class); works whereas new Intent().setComponent(componentName) fails


I have an activity which I declare in my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my">
  <uses-sdk android:minSdkVersion="14"/>
  <application>
    <activity
        android:name="com.my.InternalDummyActivity"
        android:exported="false">
    </activity>
  </application>
</manifest>

I have tried without the exported=false as well

The application contains an inner library with another activity.

I try to call an explicit intent from the other activity (other namespace) But I always get an exception:

Intent intent1 = new Intent(activity.getApplicationContext(), InternalDummyActivity.class);
activity.startActivity(intent1);


ComponentName componentName2 =
    new ComponentName(
        "com.my","com.my.InternalDummyActivity");
Intent intent2 = new Intent().setComponent(componentName2); //dones work
activity.startActivity(intent2);


Process: com.comp.outter, PID: 9267
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.my/com.my.InternalDummyActivity}; have you declared this activity in your AndroidManifest.xml?

How can I fix this?


Solution

  • Use the ComponentName's contructor that takes a Context as parameter:

    ComponentName cn = new ComponentName(context,
    "com.my.InternalDummyActivity");
    

    For some reason, you can use the contructor taking two Strings only if you know the class dynamically.