Confusion regarding action and category - android

I am new to the android as a programmer.In the android manifest file we have the code as:
<activity
android:name".Class1"
android:label="Myapp">
<intent-filter>
<action android:name="android.intent.action.MAIN/>
<category android:name="android.intent.category.LAUNCHER/>
</intent-filter>
</activity>
Question
Why do we need to specify the category LAUNCHER, when we have specified the action to be MAIN which is enough to tell the android that Class1 is going to be initial activity of my app?

ACTION_MAIN is required Action to be performed.It is the main or starting point and wont expect any data.
CATEGORY_LAUNCHER The activity is the initial activity of a task and is listed in the system's application launcher.
You could refer these links
http://developer.android.com/reference/android/content/Intent.html
What is the meaning of android.intent.action.MAIN?

"android.intent.category.LAUNCHER" means this activity will display an icon in the launcher home screen. Without this category , you can't see its icon in launcher home screen.
android.intent.action.MAIN means this activity is a entry point of your app.

Related

What is the meaning of android.intent.action.MAIN?

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" />
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.
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..
http://developer.android.com/reference/android/content/Intent.html
http://developer.android.com/guide/topics/manifest/action-element.html
<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/
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

Android Manifest

I found a code on internet for displaying a splash screen before starting an android application. He made some changes in the code which he didn't explain well.
He used Launcher for Splash screen Activity and used Default in main android Activity.
He used Package name with main class name instead of pre-generated code for android:name in action.
Here is the code.
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StartingPoint"
android:label="#string/title_activity_starting_point" >
<intent-filter>
<action android:name="com.alfred.splashscreenwithsound.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
So my question
What is the difference between LAUNCHER & DEFAULT.
Is there any good behind changing the android:name in action to the package name.
when you write LAUNCHER it will launch application with Icon and if you remove it it will not show you the application Icon
"android.intent.category.LAUNCHER" serves as our main entry into the app.
android.intent.category.DEFAULT be set if the Activity should be an option for the default action (center press) to perform on a piece of data.
Refer this
LAUNCHER is your main page DEFAULT is your Activity page when load the app. on device first LAUNCHER will open. Then If you trigger your activity page DEFAULT will open.

Implicit intent within same application

How do I build an intent and the Manifest so that I can invoke an activity with my application as an implicit intent.
The idea is that I am writing some general code that I wish to use in a number of applications.
This code is held in an Android library and is linked to the using application.
This general code opens an activity and does some work.
Once it is done (user clicks a button) it needs to transfer to an activity that is specific to this application.
As per my understanding on your questions,
You can declare your activity with specified implicit intent in your application's manifest file..
For example: If you want to make a application for view Image with your application and you have to use that application on your device which can allow other activity to view image using implicit intent action.VIEW so ,just
<activity android:label="#string/app_name" android:name=".MyImageViewerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW">
<category android:name="android.intent.category.DEFAULT">
<data android:mimetype="image/*">
</data></category></action></intent-filter>
</activity>
In the above code we can see that intent-filter has following properties.
a. action: type for which this activity will respond for.Like this activity will respond for view action.
b. category: implicit intents are divided in categories.Like this activity is in default category.
c. data: Specify more information about intent-filter. Like this activity will handle file of mimeType “image/*”.
And when you install this application in your device and click on any image file, you will see a choice dialog that offer you to select between default application and your application. Just select your application to see the image.
For more info with example look at this What is intent filter in android?
Tutorial: Registering via Intentfilter
I think you can use intent-filters. I didn't use it for such things, but I hope that will help you.
Idea is that you can call your activity not by name, but by it's action (that is setted in intent-filter in your Manifest)

Android Program Flow Help

Consider the following:
I have three packages:
package.a
package.b
package.c
Each package contains a class and an activity
package.a>>activity.a>>class.a
package.b>>activity.b>>class.b
package.c>>activity.c>>class.c
I am assembling all of these together into one application and I want activity.c to be the first activity in the stack. In other words, I want it to run first. Do I simply modify the order in the manifest? What initializes the first application and what sets their corresponding order? After the first activity is started can I just go back and forth with intents?
Thanks for your help!
You want to use the CATEGORY_LAUNCHER intent filter on the activity you want to be launched when the application starts.
From the documentation:
CATEGORY_LAUNCHER The activity can be the initial activity of a task and is listed in the top-level application launcher.
Once the application is started, you can move back and forth using intents.
You need to set your activity C with this intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This way that will be the only activity launched from the home screen.

Android: Why is the name of the android app taken as the name of the starting activity?

In my manifest file, I have given the application name as MyApp and the name of the starting activity as Main Menu.
<application android:theme="#style/theme" android:icon="#drawable/myicon" android:label="#string/app_name">
<activity android:name=".MainMenu"
android:label="#string/mainmenu_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When I see the app icon in the default menu screen of android, the name shown is Main Menu. Why is MyApp not the name shown for the app?
Because your app can define more than one activity that shows up in the launcher. All activities that have this type of intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Will appear in the launcher, each with its own label. The fact that if you don't define a label on your activity, it's inherited from the application is basically a commodity.
Also, every activity has its own android:icon (which, again, is inherited from <application> for commodity). With this property your two launcher entries can have not only different labels, but different icons as well.
On a more general note, even "non-main" activities can handle certain types of events, and in these cases their labels and icons are relevant. For example, if one of your activities (doesn't matter which one) handles ACTION_SEND intents (like most sharing apps do -- see Facebook, Twitter, ...), then whenever the user presses a "Share" button in any application, a list of applications that can complete that certain activity is displayed to the user (in this case these applications could be Facebook, Twitter and your app). This list will contain an icon and a label (i.e. Facebook). Your entry's display will be dictated by the android:icon and android:label attributes defined by your receiving <activity>, and only if it doesn't define them they'll be taken from your <application>.
This allows you to have an entry such as "Share via MyApp" without modifying your application's name. Or, if your application also has an activity that handles ACTION_VIEW, that activity could have, for example, the label "View in MyApp".
Digging even deeper, <intent-filter> elements can have their own label and icon. Basically, Android chooses the icon and label to display like this:
If the <intent-filter> has it, pick that, stop.
If the <activity> has it, pick that, stop.
Pick the one in <application>.
This is helpful in two situations:
You don't want to change your activity's label / icon: maybe you want to use those in other places such as the title bar (and you don't want your title bar to say "Share via MyApp")
You have activities with multiple intent filters. For example, you could handle both ACTION_SEND and ACTION_VIEW intents in the same activity. In this case, you'd have two intent filters set for that activity with the labels "Share via MyApp" and "View in MyApp".
I hope I've made myself clear, these are very basic things that can be pretty hard to grasp at first, but that provide great flexibility in building apps and integrating them with the Android system and/or other apps.
Another thing to note is that you can easily have an application that doesn't have any entries in the launcher (by not having any activity with the intent filter mentioned above). These are perfectly valid apps and it's good practice when creating Service-only apps (such as widgets).
If android:label is set for activity then its value is taken. It it is omitted for activity then the android:label attribute set on application is taken instead.
You simply cannot have different labels for activity and for application as your application has one launcher for your activity with the label you set it to have. I'm not quite sure I totally understand what you are trying to achieve tho.
In the manifest, remove the android:label attribute on the launch activity. Then the activity will inherit the name specified in the application's android:label.

Categories

Resources