The Inverse: <intent-filter>
When we use an explicit Intent
with startActivity()
, it is fairly clear how Android determines what activity to display: it is the one whose class is listed in the Intent
.
When we use an implicit Intent
— one with an action string and maybe a Uri
, but no class — somehow Android needs to find out what activity (or activities) can handle that.
This is where the <intent-filter>
comes in.
In our manifest, inside of an <activity>
element, you can optionally have one or more <intent-filter>
elements. These describe certain implicit Intent
structures that our activity can handle. If some code tries starting an implicit Intent
with a matching structure, our activity will be considered a candidate. So, in the case of our ACTION_PICK
and ACTION_VIEW
requests in the preceding sections, we are hoping that there are 1+ activities with an <intent-filter>
that matches our Intent
.
Most apps will have an activity with one particular <intent-filter>
:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This would match an Intent
like:
Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
A “category” is simply an identifier of related Intent
structures and is one facet of an implicit Intent
, along with things like the action string and “data” Uri
.
A launcher can ask Android “what activities support this Intent
?”. Android will return a list of matches, and the launcher can use this to provide options for the user. When the user clicks on a specific activity, then the launcher has the details to create an explicit Intent
, which it can use to start the activity. Since this is the standard behavior of launchers, and since most Android apps want to have 1+ activities appear in the launcher, most apps will have 1+ activities with the MAIN
/LAUNCHER
<intent-filter>
.
Most apps will not need <intent-filter>
on an <activity>
beyond this one, though. However, they might use other apps that have activities with other <intent-filter>
options, such as the ACTION_PICK
and ACTION_VIEW
scenarios.
We will see an example of this sort of structure in an upcoming chapter, where we will examine an app that supports ACTION_SEND
, the standard Intent
action for “share” options in apps.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.