What is the meaning of android.intent.action.MAIN? - android

I have seen so many different confusing explenations..
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
What is the meaning of
<action android:name="android.intent.action.MAIN" />
and
<category android:name="android.intent.category.LAUNCHER" />
and
<category android:name="android.intent.category.DEFAULT" />

ACTION_MAIN is considered an entry point for the application. Usually, it combines with CATEGORY_LAUNCHER in an <intent-filter> to indicate an activity that should appear in the home screen's launcher, or in anything else that considers itself to be a launcher. Such "launchers" can query PackageManager, using queryIntentActivities(), to find such activities and display them to the user.
However, ACTION_MAIN can be used in combination with other categories for other specialized purposes. For example, CATEGORY_CAR_DOCK with ACTION_MAIN indicates an activity that should be considered a candidate to be shown when the user drops their phone into a manufacturer-supplied car dock.
When an Intent is used with startActivity(), if the Intent is not already placed into a category, it is placed into CATEGORY_DEFAULT. Hence, an <activity> <intent-filter> needs to specify some <category>, using <category android:name="android.intent.category.DEFAULT" /> if nothing else.

android.intent.action.MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.
From the docs
ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.
Also,from here
Activity Action Start as a main entry point, does not expect to
receive data.
android.intent.category.DEFAULT is mainly used for implicit intents. If your activity wishes to be started by an implicit intent it should include this catetory in its filter.
If your Activity might be started by an implicit Intent when no specific category is assigned to it, its Intent filter should include this category.
android.intent.category.LAUNCHER
category -- Gives additional information about the action to execute.
CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application
See the docs..
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/guide/topics/manifest/action-element.html

<action android:name="android.intent.action.MAIN"/>
Is the main activity for this application
<category android:name="android.intent.category.LAUNCHER" />
It is in the LAUNCHER category, meaning it gets an icon in anything
that thinks of itself as a “launcher”, such as the home screen
<category android:name="android.intent.category.DEFAULT" />
The call to startActivity() will always add the DEFAULT category if
no other category is specified.
Generally just add android.intent.category.DEFAULT even if you have other Categories. This will guarantee that if Requesting Intent doesn't provide any Categories while starting the intent using startActivity(intent), then your Receiving Activity can also receive those Intents..
Source: The Busy Coders Guide to Android Development
https://commonsware.com/Android/

The answers above are pretty good, so I will just fill in some of the blanks.
<action / > element
So we all know that when the android system opens our app, it will send out an Implicit Intent and as the documentation states:
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.
Now each intent-filter specifies the type of intents it accepts based on the intent's based on the intent's <action>, <category/> and <data/>
So with <action>and <category/> we are defining the name and category of the intent our activity can accept

Related

Difference between having intent.category.HOME alone in AndroidManifest.xml and also with intent.category.DEFAULT at the same time

What are the differences between:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
</intent-filter>
and:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
See the answer Here
What is the meaning of android.intent.action.MAIN?
android.intent.action.MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.
From the docs
ACTION_MAIN with category CATEGORY_HOME -- Launch the home screen.
Also,from here
Activity Action Start as a main entry point, does not expect to
receive data.
android.intent.category.DEFAULT is mainly used for implicit intents. If your activity wishes to be started by an implicit intent it should include this catetory in its filter.
If your Activity might be started by an implicit Intent when no specific category is assigned to it, its Intent filter should include this category.
See the docs..
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/guide/topics/manifest/action-element.html
and Another View of #CommonsWare .... in that answer also.... See it
So that ACTION_MAIN is considered an entry point for the application.
Usually, it combines with CATEGORY_LAUNCHER in an <intent-filter> to indicate an activity that should appear in the home screen's launcher, or in anything else that considers itself to be a launcher. Such "launchers" can query PackageManager, using queryIntentActivities(), to find such activities and display them to the user.
However, ACTION_MAIN can be used in combination with other categories for other specialized purposes. For example, CATEGORY_CAR_DOCK with ACTION_MAIN indicates an activity that should be considered a candidate to be shown when the user drops their phone into a manufacturer-supplied car dock.
When an Intent is used with startActivity(), if the Intent is not already placed into a category, it is placed into CATEGORY_DEFAULT. Hence, an <activity> <intent-filter> needs to specify some <category>, using <category android:name="android.intent.category.DEFAULT" /> if nothing else.

How does Android different between multiple components that have same action and category?

I looked up intent filters and found that they will be used when "Android 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"(http://developer.android.com/guide/components/intents-filters.html#Building)
In my manifest file, I have
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
which from reading that guide means that this activity can handle an implicit intent with action of main and category of launcher.
However what if i have multiple applications with the same intent filter in the manifest file. I know that some implicit intent will be called with action of main and category of launcher. How does the Android O.S know to choose this application?
when you have multiple activities defined with same intent filter(action=main and category=launcher), then android takes the first activity defined in the hierarchy with that intent filter (action=main and category=launcher) and will launch it when user clicks on app icon.

Multiple android.intent.action.MAIN in manifest xml file

I am new to Android development I seen lots of tutorial where they have only android.intent.action.MAIN which is basically a start activity of the application.
But, in the android app demos, I have seen multiple android.intent.action.MAIN statements in mainfest.xml. Can anyone explain why the mainfest.xml has multiple android.intent.action.MAIN statements?
And, in which scenarios we are supposed to have multiple MAINs in manifest.xml?
They're different entry points into the program. For instance, I just created two activities, both of which had the typical intent filter
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
It turns out that my launcher screen now has two different icons for the same program, one for each different activity. This makes sense, since the MAIN/LAUNCHER intent filter essentially tells android that the activity is the app's starting activity. Nothing in android's intent filter model forces each app to have one and only one starting activity.
LAUNCHER is the category, while MAIN is the action.
MAIN defines an entry point, specifying a way the app can be launched. For example, Consider an App which has 3 activities,
SplashActivity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
EmailActivity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.CATEGORY_APP_EMAIL" />
</intent-filter>
PlayMusicActivity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.CATEGORY_APP_MUSIC" />
</intent-filter>
If I click on the app icon on the home screen i.e launcher, I see Splash Activity.
If I click on a email address from a Messaging app, the system will show me a list of apps that support sending mail including my app(basically it searches for a specific combination of the intent filter provided by all applications) And when I select my app, EmailActivity will be launched.
If I open a file browser and click on an audio file, my app will be listed to open this file, and PlayMusicActivity will be opened.
So, ACTION.MAIN is the different 'enrty' points or the different 'ways' the app can be launched.
CATEGORY will tell what type of launch it can be.

When beside launch will android.intent.action.MAIN be fired by Android

If an Activity has the following filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Under what circumstances will an Intent matching the above filter be issued by Android?
1) During launch of the application.
2) ? Are there any other circumstances?
Anything can use that filter. The OS never uses that filter, AFAIK. Home screen applications typically use that filter. Other applications can use that filter if they so choose -- for example, here is a sample app that creates its own launcher-style list.

Default when using multiple Actions in Intent-Filter

Trying to grok intents and actions in android and looking through the documentation.
But one thing I keep seeing is an intent filter with multiple actions defined. Like this, from the above link:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
But, if you call that activity, how does it choose which action is chosen?
For that matter, that linked to example has multiple activities that all contain the same actions, "android.intent.action.VIEW" for example. When calling this with something like content://com.google.provider.NotePad/notes how does it even know which activity to use?
But, if you call that activity, how
does it choose which action is chosen?
The Intent has an action. If that action matches one of the three in the Intent filter, and matches on the category, and matches on the MIME type, then it will match the Intent filter overall and will start the activity.
In other words, multiple actions (or any other element) are a logical OR.
For that matter, that linked to
example has multiple activities that
all contain the same actions,
"android.intent.action.VIEW" for
example.
And generally there is stuff in the Intent filters to distinguish one from the next.
When calling this with something like
content://com.google.provider.NotePad/notes
how does it even know which activity
to use?
It asks the content provider, "yo, dawg -- what's the MIME type for this, yo?". Given the MIME type from the content provider, it can find any matching Intent filters.

Categories

Resources