Android – Understanding which Activity starts first in an Android app

androidandroid-activity

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?

Best Answer

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