What are intent-filters in Android?

AndroidAndroid IntentIntentfilter

Android Problem Overview


In my android app, I wanted to start an activity 'B' from initial activity 'A'. I have created classes for both of these. However when using following code to start B, I get a runtime error: application has stopped unexpectedly, try again. Here is my code:

Intent myIntent = new Intent(this, AddNewActivity.class);
startActivity(myIntent); 

When I added a new entry in AndroidManifest.xml/manifest/application/activity/intent-filers for activity B then the application worked.

I have two questions:

  • When there are multiple activities entries in AndroidManifest.xml, how does android know which activity to start first?
  • I could not understand intent-filters. Can anyone please explain.

Here is my partial AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".ListAllActivity"
              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:name=".AddNewActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Android Solutions


Solution 1 - Android

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive.

When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object.

AndroidManifest.xml

<activity android:name=".HelloWorld"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http" android:host="androidium.org"/>
    </intent-filter>
</activity>

Launch HelloWorld

Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse("http://androidium.org"));
startActivity(intent);

Solution 2 - Android

> When there are multiple activities > entries in AndroidManifest.xml, how > does android know which activity to > start first?

There is no "first". In your case, with your manifest as shown, you will have two icons in your launcher. Whichever one the user taps on is the one that gets launched.

> I could not understand intent-filters. > Can anyone please explain.

There is quite a bit of documentation on the subject. Please consider reading that, then asking more specific questions.

Also, when you get "application has stopped unexpectedly, try again", use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine the Java stack trace associated with the error.

Solution 3 - Android

When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent. Likewise, if you do not declare any intent filters for an activity, then it can be started only with an explicit intent.

According: Intents and Intent Filters

Solution 4 - Android

First change the xml, mark your second activity as DEFAULT

<activity android:name=".AddNewActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Now you can initiate this activity using StartActivity method.

Solution 5 - Android

The Activity which you want it to be the very first screen if your app is opened, then mention it as LAUNCHER in the intent category and remaining activities mention Default in intent category.

For example :- There is 2 activity A and B
The activity A is LAUNCHER so make it as LAUNCHER in the intent Category and B is child for Activity A so make it as DEFAULT.

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".ListAllActivity"
              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:name=".AddNewActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

Solution 6 - Android

intent filter is expression which present in manifest in your app that specify the type of intents that the component is to receive. If component does not have any intent filter it can receive explicit intent. If component with filter then receive both implicit and explicit intent

Solution 7 - Android

Keep the first intent filter with keys MAIN and LAUNCHER and add another as ANY_NAME and DEFAULT.

Your LAUNCHER will be activity A and DEFAULT will be your activity B.

Solution 8 - Android

There can be no two Lancher AFAIK. Logcat is a usefull tool to debug and check application/machine status in the behind. it will be automatic while switching from one activity to another activity.

Solution 9 - Android

When there are multiple activities set as main and launcher with an intent filter in the manifest. Then first activity considers as Launcher activity, and android launch or open the first activity.

<category android:name="android.intent.category.LAUNCHER" />  

The above code makes an app icon available in the device menu, so if we declare 2 launcher activity in the manifest, there will be 2 app icons get created in the device app menu.

So there will be 2 app icons, on click of the first icon, first declared activity in manifest will be launch, and click of another second declared activity gets launch

Solution 10 - Android

If possible try this one instant solution:

Intent intent =new Intent(getApplicationBaseContext,second_Act.class);
StartActivity(intent);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionankitjaininfoView Question on Stackoverflow
Solution 1 - AndroidsantoshpatmcaView Answer on Stackoverflow
Solution 2 - AndroidCommonsWareView Answer on Stackoverflow
Solution 3 - AndroidMichael HorojanskiView Answer on Stackoverflow
Solution 4 - AndroidExcel LogixView Answer on Stackoverflow
Solution 5 - AndroidMahadevappa TView Answer on Stackoverflow
Solution 6 - AndroidBajirang namadeView Answer on Stackoverflow
Solution 7 - AndroidPrashantView Answer on Stackoverflow
Solution 8 - AndroidSaikat BiswasView Answer on Stackoverflow
Solution 9 - AndroidAnand SaggamView Answer on Stackoverflow
Solution 10 - AndroidShiva-mamaaView Answer on Stackoverflow