What is the purpose of "android.intent.category.DEFAULT"?
Asked Answered
G

8

123

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

Graf answered 20/4, 2011 at 8:57 Comment(6)
Your saying this is the default entry point for your application. See: developer.android.com/reference/android/content/…Monachism
@Blundell: so if an implicit intent arrives , an activity with default as its cateory in the intent filter will be called?Graf
I believe it's used to imply this is the activity to use yes "it is for use in intent filters specified in packages"Monachism
if more than one activity contain default as their category ,in their respective intent filters which activity will be called?Graf
If they have different intent filters, it will depend on the intent.Monachism
@pravy, don't forget to change the accepted answer to the correct one.Overjoy
S
139

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 ! "

Sternway answered 21/1, 2014 at 11:41 Comment(8)
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...Cobelligerent
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 activityPeridium
@Peridium 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."Overjoy
Yes pretty much. You could try it out for yourself :) .Peridium
@Cobelligerent I agree with you I don't know why some group of people find it better to explain in an incomprehensive wayAsclepiadaceous
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.Shiksa
@humblerookie. I've tried it and it does not work without DEFAULT category. If you don't believe me test for yourself.Rhotacism
If only all tutorials were written this way. Why do we still use wooden language in 2018?Nonah
R
36

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

Ruching answered 1/9, 2013 at 13:32 Comment(3)
@Comptrol, your answer is no longer below. It's rising. :-)Wilkes
Yes, but I rolled it back to the state it was . So I didn't change aypnything at all, overall.Overjoy
I find this answer more helpful to my current situation-understanding intent filters. thank you.Weimer
B
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.

Braxton answered 29/6, 2017 at 6:3 Comment(0)
P
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.

Paperhanger answered 17/7, 2013 at 3:35 Comment(2)
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.Paperhanger
This is unclear or incomplete.Pegu
T
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

Tarrasa answered 5/10, 2018 at 6:53 Comment(0)
S
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.

Scarfskin answered 2/1, 2019 at 16:16 Comment(1)
duplicates an earlier answerPegu
M
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

Maricela answered 5/12, 2020 at 19:20 Comment(1)
A clearer way of describing the category test might be "every category in the Intent must also be in the intent-filter". (IMHO, The phrase "match the exact same category" seems a slightly odd way of saying that. I'm not fond of the wording in the doc either.)Pegu
L
-1

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.

Luddite answered 22/4, 2011 at 7:26 Comment(2)
If you look at the Android Notepad example, all the Activities are marked as default: developer.android.com/reference/android/content/Intent.htmlOffenseless
"It is used to declare some operation as default action" - Not really. Or perhaps you need to give a full, concrete example in code (XML), plus the sequence of user actions, to show what you mean.Pegu

© 2022 - 2024 — McMap. All rights reserved.