How Android Resolves Intent Filters

The process of deciding which Activity to start when an implicit Intent is passed in to start Activity is called intent resolution. The aim of intent resolution is to fi nd the best Intent Filter match possible by means of the following process:

  1. Android puts together a list of all the Intent Filters available from the installed packages
  2. Intent Filters that do not match the action or category associated with the Intent being resolved are removed from the list.
    1. Action matches are made only if the Intent Filter includes the specified action. An Intent Filter will fail the action match check if none of its actions matches the one specified by the Intent.
    2. For category matching, Intent Filters must include all the categories defined in the resolving Intent, but can include additional categories not included in the Intent. An Intent Filter with no categories specified matches only Intents with no categories.
  • Each part of the Intent’s data URI is compared to the Intent Filter’s data tag. If the Intent Filter specifies a scheme, host/authority, path, or MIME type, these values are compared to the Intent’s URI. Any mismatch will remove the Intent Filter from the list. Specifying no data values in an Intent Filter will result in a match with all Intent data values.
    • The MIME type is the data type of the data being matched. When matching data types, you can use wildcards to match subtypes (e.g., earthquakes/*). If the Intent Filter specifies a data type, it must match the Intent; specifying no data types results in a match with all of them
    • The scheme is the “protocol” part of the URI (e.g., http:, mailto:, or tel:).
    • The hostname or data authority is the section of the URI between the scheme and the path (e.g., developer.android.com). For a hostname to match, the Intent Filter’s scheme must also pass
    • The data path is what comes after the authority (e.g., /training). A path can match only if the scheme and hostname parts of the data tag also match.
  • When you implicitly start an Activity, if more than one component is resolved from this process, all the matching possibilities are offered to the user. For Broadcast Receivers, each matching Receiver will receive the broadcast Intent.
  • Native Android application components are part of the intent-resolution process in exactly the same way as third-party applications. They do not have a higher priority and can be completely replaced with new Activities that declare Intent Filters that service the same actions.

Finding the launch Intent in an Activity

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 Intent intent = getIntent();
 String action = intent.getAction();
 Uri data = intent.getData();
}
Use the getData and getAction methods to fi nd the data and action, respectively, associated with 
the Intent. Use the type-safe get<type>Extra methods to extract additional information stored in 
its extras Bundle. 

The getIntent method will always return the initial Intent used to create the Activity. In some circumstances your Activity may continue to receive Intents after it has been launched. You can use widgets and Notifications to provide shortcuts to displaying data within your Activity that may still be running, though not visible

Override the onNewIntent handler within your Activity to receive and handle new Intents after the Activity has been created.

@Override
public void onNewIntent(Intent newIntent) {
 // TODO React to the new Intent
 super.onNewIntent(newIntent);
}

Passing on Responsibility

To pass responsibility for action handling to the next best Activity, use

startNextMatchingActivity

Intent intent = getIntent();
if (isDuringBreak)
 startNextMatchingActivity(intent);

This lets you add additional conditions to your components that restrict their use beyond the ability of the Intent Filter-based intent-resolution process.

Leave a Comment