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)