Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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)

The shortcut

Run the intent through three gates in order: action? categories? data? One "no" ends it.

A browser shares "Fiddle leaf fig"Step 1 of 4
  1. Gate 1: action

    Intent action is SEND; the filter lists SEND. Pass.

If the browser shared an image (image/jpeg), gate 3 would fail and Sprout would not be listed.

Drills

Answer fast — trust the rule.

quick drill

Where are intent filters declared?

quick drill

A filter has action SEND and data text/plain but no category. Why will startActivity never pick it?

quick drill

Sprout's filter accepts text/plain. Someone shares a photo (image/jpeg) with ACTION_SEND. Is Sprout offered?

quick drill

What does android:exported="true" on an activity mean?

Remember

  • Action, category, data: pass all three checks or you are not offered.
  • Filters for startActivity must include DEFAULT.
  • Set exported deliberately: true only if other apps should reach the screen.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainReceive a shared plant name

You want Sprout to appear in the share sheet when someone shares plain text from another app, so the text becomes a new plant's name. Write out the intent filter you would add to AddPlantActivity and explain each part, including the exported attribute.