How to differentiate implicit and explicit intents in Android

Implicit and explicit intents in Android

Android Intent is a messaging object used to switch between different android components. An Intent’s most common use is to launch a new activity from the current activity.

The startActivity() method is used to call an Activity.

Use of intent

The primary element of an Android application is Android Intent. However, applications can also be built without it.

Some general functions of Intent are as follows:

  • Starting a service.
  • Launching an activity.
  • Displaying a web page.
  • Displaying a list.
  • Broadcasting a message.

Intent structure

To call an Intent, we either include an <intent-filter> or use a flag. An Intent contains the following primary information:

  • Action: This is a string that states a general action to perform such as, ACTION_VIEW, ACTION_EDIT, or ACTION_MAIN .
  • Data: This is the data that the Intent works on. This can be a record from a database.

Types of Intent

There are two types of Intent. These are as follows:

  1. Implicit Intent
  2. Explicit Intent

Implicit Intent

Implicit Intent states the action to be performed. It is not responsible for calling specific in-app components.

For example, if the user wants to see a location on a map, we can use an Implicit Intent to switch to another app that displays a specific location on a map.

Code example

To create an Implicit Intent, create a MainActivity from which we can call the Intent. We then add the Intent in setOnClickListener. An example of Implicit Intent is given below:

MainActivity.kt
AndroidManifest.xml
activity_main.xml
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.btn)
button.setOnClickListener{
intent = Intent(Intent.ACTION_VIEW)
intent.setData(Uri.parse("https://www.educative.io/"))
startActivity(intent)
}
}
}
Implementation of implicit intent

Explanation

  • Line 11: button assigns the value ID used in XML file.
  • Line 14: intent assigns an action.
  • Line 15: User redirects to a page.
  • Line 16: Intent is invoked.

Explicit Intent

Explicit Intent is used to invoke a specific target component. It is used to switch from one activity to another in the same application. It is also used to pass data by invoking the external class.

For example, we can use an explicit intent to start a new activity when the user invokes an action or plays music in the background.

Code example

To create an explicit Intent, we create an activity that works as our MainActivity. We create another activity named NextActivity that the explicit intent will direct to. We then join both of these activities with a button. An example of Explicit Intent is shown below:

MainActivity.kt
AndroidManifest.xml
activity_main.xml
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.btn)
button.setOnClickListener{
startActivity(Intent(this, NextActivity::class.java))
}
}
}
Implementation of explicit intent

Explanation of MainPage

  • Line 11: button assigns the value ID used in XML file.
  • Line 14: Intent is invoked.

Free Resources