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:
- Action — the intent's action must be one of the filter's actions.
- Category — every category on the intent must appear in the filter.
startActivityaddsCATEGORY_DEFAULTto implicit intents, so a filter meant for them must listDEFAULT. - Data — the intent's URI and MIME type must match the filter's
datarules.
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.
Gate 1: action
Intent action is
SEND; the filter listsSEND. 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.
Where are intent filters declared?
A filter has action SEND and data text/plain but no category. Why will startActivity never pick it?
Sprout's filter accepts text/plain. Someone shares a photo (image/jpeg) with ACTION_SEND. Is Sprout offered?
What does android:exported="true" on an activity mean?
Remember
- Action, category, data: pass all three checks or you are not offered.
- Filters for
startActivitymust includeDEFAULT. - Set
exporteddeliberately: true only if other apps should reach the screen.
