Search code examples
androidactionactivitynotfoundexceptionandroid-implicit-intent

Implicit Intent with user-specified action


I'm experimenting, specifying my own action for use in an implicit intent. In a single package, I define two activities. ActivityTwo is to be called from onClick() in ActivityOne, using an implicit intent with an action "course.labs.activitylab.MY_ACTION". But I haven't been able to make it work.

In strings.xml:

<string name="myfunnystring">course.labs.activitylab.MY_ACTION</string>

In AndroidManifest.xml:

    <activity
        android:name=".ActivityTwo"
        android:label="@string/title_activity_activity_two" >
        <intent-filter>
            <action android:name="@string/myfunnystring" />
        </intent-filter>
    </activity>

In onClick() in the OnClickListener() in onCreate() in ActivityOne.java:

            Intent intent = new Intent();
            intent.setAction(getString(R.string.myfunnystring));
            intent.setFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION);
            startActivity(intent);

The program crashes in the emulator, and I find this in the logcat window:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=course.labs.activitylab.MY_ACTION flg=0x8 }

What am I doing wrong?


Solution

  • Add the default category to your intent filter.

    <intent-filter>
        <action android:name="course.labs.activitylab.MY_ACTION" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>