Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding which Activity starts first in an Android app

Tags:

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?

like image 702
Johann Avatar asked Jun 03 '11 13:06

Johann


People also ask

How does Android know which activity to run first?

Unlike programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.

Where do you define the starting activity of your application in Android?

In Android, you can configure the starting activity (default activity) of your application via following “intent-filter” in “AndroidManifest. xml“. See following code snippet to configure a activity class “logoActivity” as the default activity.

Where would we specify which activity should launch first in app?

The intent-filter inside the activity tells Android which Activity to launch.


1 Answers

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

like image 52
Balaji Khadake Avatar answered Sep 23 '22 01:09

Balaji Khadake