What Is the Meaning of Android.Intent.Action.Main

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.

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..

  1. http://developer.android.com/reference/android/content/Intent.html
  2. http://developer.android.com/guide/topics/manifest/action-element.html

Why is action android:name=android.intent.action.VIEW added into AndroidManifest.xml when there is a android:name=android.intent.action.MAIN

But from the following project, I find is added into AndroidManifest.xml when there is a , why?

From the documentation that explains this.

You can create a filter that includes more than one instance of <action>, <data>, or <category>. If you do, you need to be certain that the component can handle any and all combinations of those filter elements.

In other words, this is a shortcut to indicate that the app handles an intent that either has the MAIN action or the VIEW action, with the LAUNCHER category.

In other other words, this:

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Is equivalent to this:

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

The documentation also provides an example.

Android action.MAIN and category.LAUNCHER function

From the docs:

category -- Gives additional
information about the action to
execute. For example,
CATEGORY_LAUNCHER means it should
appear in the Launcher as a top-level
application, while
CATEGORY_ALTERNATIVE means it should
be included in a list of alternative
actions the user can perform on a
piece of data.

MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.

When I remove the action android:name=android.intent.action.MAIN / from the manifest the the application starts from the 2nd activity

manifest file only add between application tag below line.

  <activity android:name=".Main2Activity"
android:label="@string/title_activity_main2"/>

In manifest file only first activity which call on app start that activity add only intent filter other then activity only called above code.

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

Android: How Intent-Filter works?

Intent filters are supposed to be added BETWEEN the opening and closing tags of a receiver, service or activity. They signify the "implicit intents" that the app can handle. In your app menu, where you have all your apps listed, android looks for the intent Main and Launcher. Whichever apps have that as an intent filter, those get displayed, and the activity associated with Main, Launcher gets called as soon as the user opens the app.

      <activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

These two intent filters associated with my activity called MainActivity tell android to 1) Place my app in the menu. 2) Open MainActivity as soon as the user selects the app. Hence you should have only one activty with Main and Launcher as its intent filters.

For example if user selects share button and its an implicit intent, then the apps that have "share option" in the form of an filter can be called via a dialog box/ selector.

EDIT :

<

activity android:name="ShareActivity">
<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/vnd.google.panorama360+jpg"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
</intent-filter>
</activity>

The first activity, MainActivity, is the app's main entry point—the activity that opens when the user initially launches the app with the launcher icon:

The ACTION_MAIN action indicates this is the main entry point and does not expect any intent data.
The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in the system's app launcher. If the <activity> element does not specify an icon with icon, then the system uses the icon from the <application> element.

These two must be paired together in order for the activity to appear in the app launcher.

The second activity, ShareActivity, is intended to facilitate sharing text and media content. Although users might enter this activity by navigating to it from MainActivity, they can also enter ShareActivity directly from another app that issues an implicit intent matching one of the two intent filters.

http://developer.android.com/guide/components/intents-filters.html
Take a look at this site. So intent filters describe what the activity CAN do, how it CAN be started (via another app or the main launcher or a browser) and what additional functions it can do.

What does it mean when two or more activities, each having intent-filter with action=android.intent.action.ACTION_MAIN?

Android apps are composed of different components. e.g. Activity, Service, BroadcastReceiver, and ContentProvider and each component can act as an entry point of the app.

Let's take activity as an example, you have defined an activity in your application with following action

<intent-filter>
<action android:name="com.yourapp.SOME_ACTION" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

and I start an activity in my app with following intent.

Intent intent = new Intent("com.yourapp.SOME_ACTION"); // same action
startActivity(intent);

Now what will happen? System will search for activities with com.yourapp.SOME_ACTION action and if it finds one (in current scenario, it will be activity you have created with com.yourapp.SOME_ACTION in your app), it will start your app (if it is not already started) and will open the activity in your app.

See, now I can enter into your app by using Activity with com.yourapp.SOME_ACTION. Same thing happens in case of other components.

Info about Action MAIN and Category Launcher in Android Manifest

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



Related Topics



Leave a reply



Submit