Working with android I realized that implicit intents are good choice in most of cases due to their\'s flexibility. But what\'s about explicit intents? What are benefits of
From Docs:
There are two types of intents:
Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityB and then use it to directly call it.
Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.
An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters
Explicit Intent:
Use explicit intent when you know exactly which Activity
can handle your request.
Example: You have a List Activity and when you click an item in the list it opens a Detail activity. In this case, you KNOW that the details of the item can be shown or handled by DetailActivity.class
of your application.
So to perform this action you create an Intent by explicitly specifying the class name.
Intent showDeatil = new Intent(this,DetaiActivy.class);
startActivity(showDeatil);
Implicit Intent:
Use implicit intent when you don't know which activity of which application/s can handle your request.
Example: You have a link. When you click the link it should open the webpage in some browser. You DON'T KNOW exactly which Activity in which application can handle your request. You just have a vague idea that its a webpage link so it should open a webpage in some browser when someone opens it. In this case, you just specify the ACTION and then OS takes care of the rest.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
There is a term for it. It's called Intent Resolution.
In Intent resolution.
OS takes out the ACTION specified in your intent.
Goes in the PackageManager and looks up for all the registered activities with the matching ACTION all the application installed in your device.
Shows the list of all matching applications in a pop-up.
Sometimes it's possible that there will be no Activity which matches with the ACTION. In this case, you will get a NullPointerException. So a more preferred way is this
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Let's say you have written some Browser Application. If you want your applications to show up in the pop-up list when someone opens the link. Then you have to register your Activity with the action using Intent Filters AndroidManifest.xml file. Like this.
<application
..... >
......
<activity android:name=".YourBrowserActivity">
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="www.example.com" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
.....
</application>
References
Common Intent ACTIONS and their Intent-Filters list
More on Intent filters and Intent resolution
Implicit Intents do not directly specify the Android components which should be called, it only specifies action to be performed. A Uri can be used with the implicit intent to specify the data type.
for example
Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com"));
this will cause web browser to open a webpage. Android system searches for all components which are registered for the specific action and the data type.If many components are found then the user can select which component to use..
Explicit intents are used in the application itself wherein one activity can switch to other activity...Example Intent intent = new Intent(this,Target.class);
this causes switching of activity from current context to the target activity.
Explicit Intents can also be used to pass data to other activity using putExtra
method and retrieved by target activity by getIntent().getExtras()
methods.
Hope this helped.
Simply we can describe both intents like this...
Explicit Intents: They are used for communication inside a particular application.
eg: Consider an application that has a login page consisting of two Fields (say username and password).If both are true it will lead us to a page that displays the username field which we entered before. In this case, we use explicit intents because we need to change the activities and to carry data from one activity to the other activity(username field) in the same application.
Implicit Intents: They are used for communication across two different applications.
eg: consider an app that uses user's image from the camera as a profile picture. For this purpose, it uses Implicit Intent. It will call intent to camera application to get a picture from the it.
I hope you can understand.