Skip to the workspace
Pebellslearn by building
Intents and the Other Components  ›  Lesson

Intent Filters — Letting Others Find You

Intent Filters 14 minAn intent filter in the manifest declares which actions, categories and data an activity can handle, so implicit intents can reach it.

android / Intent Filters — Letting Others Find You

Explore each scene at your own pace.

01 · The rule

The rule

Intent Filters — Letting Others Find You

In the last lesson Sprout sent implicit intents. Now flip it: when someone shares a plant name from a browser, Sprout should appear in the share sheet. For the system to offer your app, you must declare what it can handle. That declaration is an intent filter.

The rule

Action, category, data: pass all three checks or you are not offered.

An intent filter lives inside an activity in the manifest:

<activity android:name=".AddPlantActivity"
          android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

The system tests an implicit intent against the filter in three checks:

  1. Action — the intent's action must be one of the filter's actions.
  2. Category — every category on the intent must appear in the filter. startActivity adds CATEGORY_DEFAULT to implicit intents, so a filter meant for them must list DEFAULT.
  3. Data — the intent's URI and MIME type must match the filter's data rules.

Fail any one, and your activity is not offered.

android:exported="true" allows other apps to start the activity. When an activity has an intent filter, you must set exported explicitly on modern Android versions. If only your own app should start a screen, leave it false and give it no filter.

Inside the activity, read what arrived:

val sharedName = intent.getStringExtra(Intent.EXTRA_TEXT)
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.