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

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.

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.

single app with different launches to configure and run in android

I have a requirement to do an app to function like this.
Its an android application and it should have 2 launching methods.
One is to proceed the function directly when touching the icon.
Another icon should be there to give the setting to the same app to set the setting the settings of the android application.
Basically the app should work like the screen off and lock app in the market.
How can i achieve these 2 things in one app? When i install the app, i need to have two icons, one to do the functionality directly and other to set the settings of the application.
I believe you are trying to have to separate launchers (icons to start apps) in your app, launching two different activities. This is easily achieved within your manifest. Create two activities, say MainActivity and SettingsActivity and then declare them in manifest as launch-able - with different titles:
<activity
android:name=".MainActivity"
android:label="#string/main_activity_title"
android:icon="#drawable/main_icon">
<intent-filter android:label="#string/main_app_title">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="#string/settings_activity_title"
android:icon="#drawable/settings_icon">
<intent-filter android:label="#string/settings_app_title">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When your app is installed, there will be two icons created: one to start the MainAcivity and another one to start SettingsActivity.

How to create additional launcher Icon(s) for same android app?

I have an application where I need to provide a additional icon acccessible in applications to launch an activity from the same application.
So that when application installs it shows two icons in the applications list, one icon to launch the app is already there, how to have the other icon and then how can I start the required activity from that icon/shortcut.
add this in the manifest file inside the activity tag which you want to show in the launcher
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Multiple Activities = multiple Apps installed?

I'm just getting started with the Android SDK and after I just understood how to open another view with a MenuItem. I'm now facing a problem that it is showing me my two activities as separated applications installed?
I've created a new activity and a new xml file (layout) and when I touch the menuItem in the ActionBar in my first view it opens the second view.
Any idea to work around that?
It sounds like you have the <intent-filter> set as
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
for both activities. This will create a launcher icon for each and make each an entry point for your app
Docs
Most likely, you've included the following intent filter in both your Activities:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This is meant to be put in only those Activities which you want shown in the app list.

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.

Categories

Resources