122

What is the purpose of using android.intent.category.DEFAULT in the Category field of Intent Filters?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Pravy
  • 2,165
  • 5
  • 22
  • 28
  • 2
    Your saying this is the default entry point for your application. See: http://developer.android.com/reference/android/content/Intent.html#CATEGORY_DEFAULT – Blundell Apr 20 '11 at 09:19
  • @Blundell: so if an implicit intent arrives , an activity with default as its cateory in the intent filter will be called? – Pravy Apr 20 '11 at 09:29
  • 1
    I believe it's used to imply this is the activity to use yes "it is for use in intent filters specified in packages" – Blundell Apr 20 '11 at 10:09
  • if more than one activity contain default as their category ,in their respective intent filters which activity will be called? – Pravy Apr 20 '11 at 10:32
  • If they have different intent filters, it will depend on the intent. – Blundell Apr 20 '11 at 10:49
  • @pravy, don't forget to change the accepted answer to the correct one. – Özgür Jul 22 '14 at 07:12

8 Answers8

133

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter). If you are sure that your activity must be called with any other Category, don't use the Default.

Setting Category to Default doesn't mean that this Activity will be used by default when your app launches. The Activity just says to system that " Oh I could be started, even if the starter Intent's category is set to Nothing at all ! "

Kushal
  • 8,100
  • 9
  • 63
  • 82
Özgür
  • 8,077
  • 2
  • 68
  • 66
  • 36
    This was actually an understandable explanation. Compare this answer to the docs: "Set if the activity should be an option for the default action (center press) to perform on a piece of data. Setting this will hide from the user any activities without it set when performing an action on some data" How hard can it be to write a comprehensible description... – Andreas Mar 25 '14 at 17:22
  • I'm not sure about its mandate though as it seems to be optional since an activity without any intent filter could still be called as an implicit activity – humblerookie Jul 15 '14 at 07:06
  • @humblerookie are you sure? Documentation says this is required: " Note also the DEFAULT category supplied here: this is required for the Context.startActivity method to resolve your activity when its component name is not explicitly specified." – Özgür Jul 15 '14 at 13:47
  • Yes pretty much. You could try it out for yourself :) . – humblerookie Jul 16 '14 at 06:43
  • @Andreas I agree with you I don't know why some group of people find it better to explain in an incomprehensive way – Alan Deep Jan 08 '15 at 15:56
  • 7
    Documentation is now more clear IMO: *Android automatically applies the the CATEGORY_DEFAULT category to all implicit intents passed to startActivity() and startActivityForResult(). So if you want your activity to receive implicit intents, it must include a category for "android.intent.category.DEFAULT" in its intent filters.* – shkschneider May 07 '15 at 08:53
  • 2
    @humblerookie. I've tried it and it does not work without DEFAULT category. If you don't believe me test for yourself. – Bob Ueland Dec 19 '15 at 14:40
  • 2
    If only all tutorials were written this way. Why do we still use wooden language in 2018? – SudoPlz Jan 04 '18 at 14:27
33

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

I think the term "default" should be understood as "default candidate". If the action on a piece of data resolves to multiple activities, then Android will present all candidates to the user and the user can select his preferred default.

Reference:

http://developer.android.com/guide/components/intents-filters.html

Extract from that page:

Android treats all implicit intents passed tostartActivity() as if they contained at least one category: "android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters. (Filters with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" settings are the exception. They mark activities that begin new tasks and that are represented on the launcher screen. They can include "android.intent.category.DEFAULT" in the list of categories, but don't need to.)

Özgür
  • 8,077
  • 2
  • 68
  • 66
Bruno Ranschaert
  • 7,428
  • 5
  • 36
  • 46
2

Activities will need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity().

In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If we do not declare it in our intent filter, no implicit intents will resolve to our activity.

Shinoo Goyal
  • 601
  • 8
  • 10
1

It is actually to make sure your other activities can be called out when the app is running. LAUNCHER will make the activity that has it the first activity that starts. To use intents to get to the other activities, they have to be listed as "actual" activities by putting DEFAULT. That is from what I know so don't quote me if it's wrong.

CHT
  • 66
  • 2
  • 1
    The reference I have is a project I worked on while I studied about Android coding. If you want the source files, I'll be happy to send them. – CHT Sep 11 '13 at 03:27
0

category:

android.intent.category.DEFAULT

Matches any implicit Intent. This category must be included for your Activity to receive any implicit Intent.

https://codelabs.developers.google.com/codelabs/android-training-activity-with-implicit-intent/index.html?index=..%2F..%2Fandroid-training#6

4b0
  • 21,981
  • 30
  • 95
  • 142
RIK
  • 123
  • 1
  • 2
  • 13
0

https://developer.android.com/guide/components/intents-filters

To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.

k4dima
  • 6,070
  • 5
  • 41
  • 39
0

It is used to declare some operation as default action (as its name suggest). Lets consider we have a notepad app(referring to android notepad sample). The first page of app consists of a list of all notepad files. When one notepad file is selected one of the operations like edit note, delete note etc can be performed. But I want to make edit as my default action which means when i press center button of my keypad,edit window should be open.

Prerna
  • 365
  • 1
  • 6
  • 16
  • 1
    If you look at the Android Notepad example, all the Activities are marked as default: http://developer.android.com/reference/android/content/Intent.html – Peter Ajtai Apr 09 '12 at 21:26
0

Before an implicit Intent is accepted by an Activity the Intent must pass a category test: Each category in the Intent must match the exact same category in the Intent-filter of the Activity.

The category DEFAULT is automatically applied to all implicit intents (by default) so because of the reason above every Activity that want to receive any implicit intent at all has to include this category in its Intent-filter.

Source

MattSchmatt
  • 850
  • 8
  • 18