Activities with same intent filter name

We already learn that, an intent filter is necessary for invoking an activity using intent object. Assume that  there is more than one activities present with same intent filter name. In this post i am going to explain this situation with a complete working example. If there is more than one activities with same intent filter name, than the android system will show a dialog window that prompt you to choose an appropriate activity to complete the action.   
In this example there are two activities SecondActivity and ThirdActivity with same intent filter name as "common_filter". The manifest code segment for this activities are shown bellow. 
1:   <activity  
2: android:name="com.testdemo.SecondActivity"
3: android:label="@string/title_activity_second" >
4: <intent-filter>
5: <action android:name="common_filter" />
6: <category android:name="android.intent.category.DEFAULT" />
7: </intent-filter>
8: </activity>
9: <activity
10: android:name="com.testdemo.ThirdActivity"
11: android:label="@string/title_activity_third"
12: >
13: <intent-filter>
14: <action android:name="common_filter" />
15: <category android:name="android.intent.category.DEFAULT" />
16: </intent-filter>
17: </activity>

MainActivity.java
1:  package com.testdemo;  
2: import android.app.Activity;
3: import android.content.Intent;
4: import android.os.Bundle;
5: import android.view.Menu;
6: import android.view.View;
7: import android.view.View.OnClickListener;
8: import android.widget.Button;
9: public class MainActivity extends Activity {
10: Button bn;
11: @Override
12: protected void onCreate(Bundle savedInstanceState) {
13: super.onCreate(savedInstanceState);
14: setContentView(R.layout.activity_main);
15: bn= (Button) findViewById(R.id.button1);
16: bn.setOnClickListener(new OnClickListener() {
17: @Override
18: public void onClick(View v) {
19: // TODO Auto-generated method stub
20: Intent i = new Intent("common_filter");
21: startActivity(i);
22: }
23: });
24: }
25: @Override
26: public boolean onCreateOptionsMenu(Menu menu) {
27: // Inflate the menu; this adds items to the action bar if it is present.
28: getMenuInflater().inflate(R.menu.main, menu);
29: return true;
30: }
31: }

SecondActivity.java
1:  package com.testdemo;  
2: import android.os.Bundle;
3: import android.app.Activity;
4: import android.view.Menu;
5: public class SecondActivity extends Activity {
6: @Override
7: protected void onCreate(Bundle savedInstanceState) {
8: super.onCreate(savedInstanceState);
9: setContentView(R.layout.activity_second);
10: }
11: @Override
12: public boolean onCreateOptionsMenu(Menu menu) {
13: // Inflate the menu; this adds items to the action bar if it is present.
14: getMenuInflater().inflate(R.menu.second, menu);
15: return true;
16: }
17: }

ThirdActivity.java
1:  package com.testdemo;  
2: import android.os.Bundle;
3: import android.app.Activity;
4: import android.view.Menu;
5: public class ThirdActivity extends Activity {
6: @Override
7: protected void onCreate(Bundle savedInstanceState) {
8: super.onCreate(savedInstanceState);
9: setContentView(R.layout.activity_third);
10: }
11: @Override
12: public boolean onCreateOptionsMenu(Menu menu) {
13: // Inflate the menu; this adds items to the action bar if it is present.
14: getMenuInflater().inflate(R.menu.third, menu);
15: return true;
16: }
17: }

AndroidManifest.xml
1:  <?xml version="1.0" encoding="utf-8"?>  
2: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3: package="com.testdemo"
4: android:versionCode="1"
5: android:versionName="1.0" >
6: <uses-sdk
7: android:minSdkVersion="8"
8: android:targetSdkVersion="17" />
9: <application
10: android:allowBackup="true"
11: android:icon="@drawable/ic_launcher"
12: android:label="@string/app_name"
13: android:theme="@style/AppTheme" >
14: <activity
15: android:name="com.testdemo.MainActivity"
16: android:label="@string/app_name" >
17: <intent-filter>
18: <action android:name="android.intent.action.MAIN" />
19: <category android:name="android.intent.category.LAUNCHER" />
20: </intent-filter>
21: </activity>
22: <activity
23: android:name="com.testdemo.SecondActivity"
24: android:label="@string/title_activity_second" >
25: <intent-filter>
26: <action android:name="common_filter" />
27: <category android:name="android.intent.category.DEFAULT" />
28: </intent-filter>
29: </activity>
30: <activity
31: android:name="com.testdemo.ThirdActivity"
32: android:label="@string/title_activity_third"
33: >
34: <intent-filter>
35: <action android:name="common_filter" />
36: <category android:name="android.intent.category.DEFAULT" />
37: </intent-filter>
38: </activity>
39: </application>
40: </manifest>