Search code examples
androidandroid-intentintentfilter

What does an IntentFilter do?


I don't quite understand what the IntentFilter does? Here is an example manifest:

<activity android:name="SomeActivity" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

What does the <action> and <category> tags do? Say if I declare:

<activity android:name="AnotherActivity" android:label="AnotherActivity">
    <intent-filter>
        <action android:name="android.intent.action.PICK" />
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.INSERT" />
    </intent-filter>
</activity>

Does that even do anything if used in conjunction with an Intent object? Please help me understand an IntentFilter?


Solution

  • To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters. Each filter describes a capability of the component, a set of intents that the component is willing to receive.

    An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as elements.

    A filter has fields that parallel the action, data, and category fields of an Intent object. An implicit intent is tested against the filter in all three areas. To be delivered to the component that owns the filter, it must pass all three tests. If it fails even one of them, the Android system won't deliver it to the component — at least not on the basis of that filter. However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another.

    For more information regarding Intent-Filter, refer this Android Developer - Intent Filters Page