Android How to - Resolve Intent Filter Collision








The <intent-filter> element defines how your activity can be invoked by another activity.

What happens if another activity in either the same or a separate application has the same filter name?

Example

For example, suppose your application has another activity named Activity3, with the following entry in the AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.java2s.app" >
/*from   w w w .j a v  a 2s  .co  m*/
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.java2s.app.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="Second Activity"
            android:name=".SecondActivity" >
            <intent-filter >
                <action android:name="com.java2s.SecondActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
        android:label="Third Activity"
        android:name=".ThirdActivity" >
        <intent-filter >
            <action android:name="com.java2s.SecondActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    </application>

</manifest>

If you call the startActivity() method with the following intent, then the Android OS will display a selection of activities:

startActivity(new Intent("com.java2s.SecondActivity"));

If you check the "Use by default for this action" item and then select an activity, then the next time the intent "com.java2s.SecondActivity" is called again, it will launch the previous activity that you have selected.





Clear

To clear this default,

  • go to the Settings application in Android
  • select Apps -> Manage applications,
  • select the application name.
  • When the details of the application are shown, scroll down to the bottom and click the Clear defaults button.