23
votes

It isn't clear to me how Android determines which Activity starts first when an app starts. The Android documentation states the following concerning the AndroidManifest.xml file about Activities:

"Only one activity should have the "main" action and "launcher" category..."

So in the AndroidManifest.xml file, you should essentially have only one:

action android:name="android.intent.action.MAIN"

category android:name="android.intent.category.DEFAULT"

However, while looking at sample code from the Android SDK, the application called "APIDemos" contains a manifest file with tons of

"android.intent.action.MAIN" and "android.intent.category.DEFAULT"

I am totally confused. This seems to go contrary to what Google is stating about there only suppose to be one. Does Android simply grab whichever one appears first in the manifest and ignores all the others? If not, why are there multiple MAINs and DEFAULTs?

3

3 Answers

10
votes

Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity(). So, CATEGORY_DEFAULT can appear number of times.

Android does not grab whichever one appears first in the manifest but it starts with activity having CATEGORY_LAUNCHER.

CATEGORY_LAUNCHER : The activity can be the initial activity of a task and is listed in the top-level application launcher.

For more details refer: http://developer.android.com/guide/topics/intents/intents-filters.html

6
votes

action.MAIN and category.LAUNCHER are the ones that are used to specify what activity gets launched when the user presses your app icon or selects it from the running list of apps.

You can use other combinations of actions and category.DEFAULT to respond to different events but the combination of action.MAIN and category.LAUNCHER should only be defined once.

6
votes

I am totally confused. This seems to go contrary to what Google is stating about there only suppose to be one.

It isn't contrary. These activities have category CATEGORY_DEFAULT, but not CATEGORY_LAUNCHER.