If Activity manager launches the MainActivity through intent-filter with action=main, and category=launcher, then which type of intent is used?
I'm confused little bit. Is it implicit intent?
An intent is an abstract description of an operation to be performed. Its most significant use is in the launching of activities.
when the user clicks on the application icon, the android system looks in the manifest file for the intent with
action="android.intent.action.MAIN"
and
category="android.intent.category.LAUNCHER".
MAIN action is the main entry point of the application.
LAUNCHER category means it should appear in the Launcher as a top-level application.
Intent is just a piece of information about intention. Intent do not starts anything. It just inform OS that there is a need to do something (i.e start app). System looks for apps able to resolve this intention, starts them and pases to them starting intent (becouse you can pass some portion of data in it).
When user clicks app icon in launcher, launcher application generates and sens intent to OS (with explicit name of desired application to start). Android creates separate DVM, main activity class, starts acrivity's life cycle with calling onCreate() and brings activity to foreground.
When the user selects your app icon from the Home screen, the system calls the onCreate() method for the Activity in your app that you've declared to be the "launcher" (or "main") activity. This is the activity that serves as the main entry point to your app's user interface.
You can define which activity to use as the main activity in the Android manifest file, AndroidManifest.xml, which is at the root of your project directory.
The main activity for your app must be declared in the manifest with an intent-filter that includes the MAIN action and LAUNCHER category(That you know probably).
If a component does not have any intent filters, it can receive only explicit intents. A component with filters can receive both explicit and implicit intents.
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.
For more details please refer this link:
http://developer.android.com/guide/components/intents-filters.html
Related
Using a navigation drawer we can use an intent in the onNavigationItemSelected method.
To navigate to our second activity from the home (content_main) activity like this:
Intent intent = new Intent("com.example.userID.atp_jag.SecondActivity");
startActivity(intent);
This transitions to the SecondActivity successfully, however:
When I use:
Intent intent = new Intent("com.example.userID.atp_jag.Home");
startActivity(intent);
The app crashes.
How do you navigate back to the home activity (the default e.g content_main) via intents?
Error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.userID.atp_jag.Home
In general, you do not want to have <intent-filter> elements in your manifest, unless you are exposing that component to other apps. So, for example, the launcher activity has an <intent-filter>, because the point of the launcher activity is to be launched by home screens.
The corollary to this is that we usually use explicit Intents for starting our own activities. Implicit Intents, with action strings, is mostly for starting activities of other apps.
In your case, the error is because you created an implicit Intent, but for an action that does not exist. Switching to an explicit Intent should resolve that.
If the intent filters are to resolve implicit intents, then why does
the MainActivity(which is the very first activity that is run when
app is launched) has an intent filter?
Who send an implicit intent to it?
What if the sent implicit intent doesn't have proper data ?
Well, how does the system know which activity is the main activity? It isn't the name- the system doesn't care about the name. Its the activity with the intent filter that says its the main activity.
It can also have other intent filters to launch it any other way you may want. For example, you may have an intent filter to launch it via a deep link.
As for proper data- if launched from the app list or homescreen, it won't have any data. Its on the programmer of the app to make sure that it can do something that makes sense in that case.
It has CATEGORY_LAUNCHER and 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.
CATEGORY_LAUNCHER tells that your activity should be displayed in the top-level launcher.
https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN
Launcher sends implicit intent to it. This is how launcher knows which activity is to be opened on click.
If you send improper data it will not open your activity. For ex:
If you try to start your main activity using implicit intent only in startActivity then it will not start because there is CATEGORY_DEFAULT associated with it. You need to add one more intent_filter to your activity to resolve the intents.
When you launch for the first time ,does the starting activity receive any intent?
If it does, where does it come from? which class starts it?
AFAIK it is Looper which is instructed by ActivityManager initiated by eg. App drawer and usually it sends intent with
action = "android.intent.action.MAIN"
and
category = "android.intent.category.LAUNCHER"
Usually there is no data (extras) attached.
You can write your own Launcher it would require do to the following:
get the list of installed packages
check if package responds to intent with fields as mentioned above.
a. If there are multiple classes which resonds to intent you need to handle all of them (add icons)
If user clicks on an icon, send intent to that package with proper intent.
I would like to go back to the Main Activity at a certain point in my application. I tried calling the Activity in an Intent as usual, however since the Activity name for the main class is called "android.intent.action.MAIN", which is the general name of every application's main activity, a "Complete Action Using:" menu pops up with every possible application on the phone as an option. I do not want this; what I want is that the main activity for my application loads up.
How can I achieve this?
Thank you in advance!
You can also specify a specific Activity to launch like so:
Intent nextActivityIntent = new Intent(this, MyActivity.class);
startActivity(nextActivityIntent);
As per the Intent documentation, the ACTION_MAIN flag is used to specify that you want to launch an application using the application's main entry point. You generally do not use it in your app unless you are trying to open another app.
What is the purpose of using android.intent.category.DEFAULT in the Category field of Intent Filters?
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 ! "
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.)
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.
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.
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.
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
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.
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