Sunday, 17 November 2013

What are Intent Filters exactly in Android?

To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters. Each filter describes a capability of the component, a set of intents that the component is willing to receive. It, in effect, filters in intents of a desired type, while filtering out unwanted intents — but only unwanted implicit intents (those that don't name a target class). An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters. If we go through an Example, So we can understand easily.
When you use an explicit intent it's like you tell Android "open SecondActivity".
When you use an implicit intent you tell Android: "open an activity that can do these things". These things is actually the filter that you write in the manifest for SecondActivity.
As an example, if you are in FirstActivity and want to start SecondActivity:
Explicitly You Can Declare Like This:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Implicitly You can Declare Like This:
Intent intent = new Intent();
And in this case you should have in your manifest file something like:
<activity android:name="SecondActivity">
      <action android:name="myAction"/>
      <category android:name="myCategory"/>

No comments:

Post a Comment