tags:

views:

22

answers:

1

I don't get the Android Intent matching concept! I must be missing something, but I read and re-read the docs and don't get it. Maybe some kind soul can shed some light on this?

I am able to start an Activity if I specify a Category filter "android.intent.category.DEFAULT" in the manifest:

    ...
    <activity 
        android:name="mmmo.android.test.ItemDetails"
        <intent-filter>
            <action android:name="android.intent.action.INSERT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    ...

and if I then don't add any Category to the Intent object:

        ...
        Intent intent = new Intent(Intent.ACTION_INSERT);
        startActivity(intent);
        ...

That works. However, as soon as I define any other category than "android.intent.category.DEFAULT" I am only getting ActivityNotFoundExceptions. E.g. if I specify:

    ...
    <activity 
        android:name="mmmo.android.test.ItemDetails"
        <intent-filter>
            <action android:name="android.intent.action.INSERT" />
                    <category android:name="foo.bar" />
        </intent-filter>
    </activity>
    ...

and then try to start that Activity using:

        ...
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.addCategory("foo.bar"); 
        startActivity(intent);
        ...

this does not work. The doc reads "... every category in the Intent object must match a category in the filter. ...". The category name I add to the Intent matches the category I specified in the filter. So why does this not match up and just throws an exception???

Michael

+1  A: 

You must also add

 <category android:name="android.intent.category.DEFAULT"></category>

to the intent filter for the intent to be resolved.

See Intent:

Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity().

Thorstenvv
As suggested I added "android.intent.category.DEFAULT" as additional category in the manifest file (but not in the code!) and then the activity was indeed found! So, that means, one always has to add the DEFAULT-category to each and every intent filter? I find this VERY weird and IMHO this is probably a bug!
mmo
from the documentation, it sounds as if that is in fact true for activities launched using startActivity. However, when used internally in an application, it is obviously not required (at least not if the activity does not have an intent filter).PS: If my answer helped you, it would be nice if you could accept / up it.
Thorstenvv