Open In App

Difference Between Implicit Intent and Explicit Intent in Android

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The intent is a messaging object which tells what kind of action to be performed. The intent’s most significant use is the launching of the activity. The intent is a passive data structure holding an abstract description of an action to be performed. 

Body of Intent:

There are two important things in intent

  • action: The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
  • data: The data to operate on, such as a person record in the contacts database, is expressed as a Uri

Note: Uniform Resource Identifier (URI) is a string of characters used to identify a resource. A URI identifies a resource either by location, or a name, or both.

Types of Android Intents

There are two types of intents in android: 

  1. Implicit and
  2. Explicit.

1. Implicit Intent

Implicit Intent doesn’t specify the component. In such a case, intent provides information on available components provided by the system that is to be invoked. For example, you may write the following code to view the webpage.

Intent intent=new Intent(Intent.ACTION_VIEW);  

intent.setData(Uri.parse(“https://www.geeksforgeeks.org”));  

startActivity(intent);  

Example:

In the below images, no component is specified, instead, an action is performed i.e. a webpage is going to be opened. As you type the name of your desired webpage and click on the ‘CLICK’ button. Your webpage is open. 

Implicit Intent

2. Explicit Intent

Explicit Intent specifies the component. In such a case, intent provides the external class to be invoked.

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  

startActivity(i);  

Example: 

In the below example, there are two activities (FirstActivity, SecondActivity). When you click on the ‘GO TO OTHER ACTIVITY’ Button in the FirstActivity, then you move to the SecondActivity. When you click on the ‘GO TO HOME ACTIVITY’ button in the SecondActivity, then you move to the first activity. This is getting done through Explicit Intent.

Note: To know more about the types of intent with example code please refer to Implicit and Explicit Intents with Examples.

Now let’s find out what are the major difference between these two.

Difference Table

Explicit Intent

Implicit Intent

As already mentioned above explicit intents are those in which the user has a clear vision and knows exactly which activity can handle the requests.

Example: When you have a Listview screen on tap of each item you will go to detail activity  

Intent = Intent(applicationContext,DetailActivity::class.java)

startActivity(intent) 

Implicit intents do not name a specific component like explicit intent, instead declare general action to perform, which allows a component from another app to handle.

Example: When you tap the share button in any app you can see the Gmail, Bluetooth, and other sharing app options. Here user sends a request is the implicit intent request which can be handled by these Gmail, Bluetooth-like app.

Explicit intent can do the specific application action which is set by the code like changing activity, downloading the file in the background, etc. It specifies the only action to be performed and does not directly specify Android Components.

In explicit intent, you can pass data to other activities by using the putExtra method and retrieve by using getIntent().

Example:

val intent = Intent(this, SecondActivity:: class.java).apply{

putExtra(“key”,”New Value”)

}

startActivity(intent)

Second Screen:

val secondIntent = intent.getStringExtra(“key”)

Here we just mention the action in the intent and OS decides which applications are suitable to handle the task, action across two different applications.
Explicit intents are used for communication inside the application. Like changing activities inside the application. They are used for communication across two different applications.
In explicit intent, the action target is delivered even the filter is not consulted. When you make an implicit call with the intent. OS look at the action and then it matches with all the filters intent-filters of all the registered activities of all application using PackageManager and then populates the result as a list, this process is called as intent Resolution. 

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads