108

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" />
Gero
  • 12,993
  • 25
  • 65
  • 106

4 Answers4

111

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.

Droidman
  • 11,485
  • 17
  • 93
  • 141
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    upvoted! I knew it that you will answer this and I have been waiting to read it as expected a clear and concise answer. thanks – Spurdow Aug 09 '14 at 14:49
  • 8
    Uhm, Still you haven't explained why we need both. I mean, ACTION MAIN = ENTRY POINT, got it. CATEGORY_LAUCHER = To indicate that an activity should appear in the home screen's launcher.Those two sentences are the same just phrased differently, arent they? – frankelot Dec 08 '15 at 04:16
  • 10
    @feresr: No, they are not. You can tell that by reading the answer, including the second paragraph, which provides an example of a *different* use of `ACTION_MAIN` that does *not* involve a home screen-style launcher. – CommonsWare Dec 08 '15 at 12:09
  • If i commented and only have in Manifest and try running the app, why there is no launcher icon in Home Screen. – vgokul129 Dec 09 '17 at 20:35
  • 2
    @vgokul129: Because home screen launchers specifically look for activities that have an `` with both `` and ``. – CommonsWare Dec 09 '17 at 20:39
  • @CommonsWare: I don't know much about launchers. I will give you a thumb up for your reply. Is it possible to create just an app icon which does nothing when clicked? To be precise during on icon click in launcher, no startActivity(intent) should call. – vgokul129 Dec 09 '17 at 22:27
  • 2
    @vgokul129: "To be precise during on icon click in launcher, no startActivity(intent) should call" -- only if you write your own home screen with your own launcher that offers this sort of capability. The behavior of a home screen launcher is up to the implementers of that home screen. Most focus on starting advertised launchable activities, since that's the principal role of a launcher. You are welcome to have an activity that calls `finish()` right away, but users will think that your app is broken. – CommonsWare Dec 09 '17 at 22:31
  • @CommonsWare "Because home screen launchers specifically look for activities that have both" > Any specific reason why vendors implementation is so ? – vgokul129 Dec 09 '17 at 22:34
  • @vgokul129: Because that's what Google advises home screen developers to do, and that's what Google has done with its home screen implementations (e.g., in the AOSP). – CommonsWare Dec 09 '17 at 22:35
  • @CommonsWare You have connected all the dots finally. Father behind the reason for son. Thanks Mark. – vgokul129 Dec 09 '17 at 22:40
  • A related question: I detect an app icon launch by checking the intent for ACTION_MAIN and CATEGORY_LAUNCHER. This is true in the case of a cold start (or when the activity was killed). How can I detect a warm start (pushing to the background and bringing it to the foreground)? I don't use a singleTask/singleInstance/singleTop for my activity. It can launch into the previously opened activity. – sr09 Mar 29 '18 at 22:51
  • @sr09: Override `onStart()` or `onResume()`, I guess. I don't really understand your concern. You might consider asking a separate Stack Overflow question. – CommonsWare Mar 29 '18 at 23:10
81

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
Lal
  • 14,726
  • 4
  • 45
  • 70
4
<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/

Aric TenEyck
  • 8,002
  • 1
  • 34
  • 48
Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
0
  • 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

Tristan Elliott
  • 594
  • 10
  • 8