Explicit Intent in Android Example

Explicit Intent in Android – Although it does not look very complex through the eyes of a user, there are hundreds of operations taking place in an Android application as the user continues using it. Out of a large population, only a few curious users would know that there are tons of interactions occurring within a single Android app at a time. If you are a newly curious user looking for information on interactions within a single app, you came to the right place.

The tool that enables an Android app to have its components interact with each other is called Android Intent. To put it in layman’s terms, an Android Intent is a bridge that connects Android components. With the help of an Android Intent, two app components communicate with each other. You must be wondering why app components need to communicate with each other.

The reason for that is simple: A single app component cannot do everything. In order to be complete, an application must be able to make use of multiple components and possess the ability to transfer data from one component to the other. And this is where Android Intent comes in.

An Android Intent is used when an application has to request an Android component in order to perform an operation for it. This component can either belong to the same application or any other application present on the device.

By its nature, the object of an Android Intent is a passive data structure (commonly known as PDS). This PDS only holds abstract description of the required operation. In other words, the Android Intent object carries the information that defines the requested operation. When the request is sent, the Android System uses the object’s information in order to select a component that is able to fulfill the intent.

Types of Android Intent

As it is mentioned before, the answering component of an Android Intent could be from the same app or a different app. This divides the Android Intent into two:

  • Implicit Intent
  • Explicit Intent

In this article, we will only discuss Explicit Intent and how to implement it in an Android application. However, as far as Implicit Intent is concerned, we have discussed it in a separate tutorial.

Explicit Intent in Android

In Android, Explicit Intent is the type of Intent that explicitly defines the name of the component to be invoked by the Intent. It means that the app knows what the targeted component is and if it exists on the device or not. For this reason, Explicit Intent in Android is most commonly used for invoking components within an application. In other words, Explicit Intent connects the internal world of an application; this is done by connecting one activity to another.

Basically, an Explicit Intent in Android specifies the targeted component directly by identifying it by its package or class name. Then, the Android System only revolves the given name and launches the targeted component directly. Usually, Explicit Intent in Android is applied in an app for various reasons, such as calling a new activity or service to be launched in response to a user action and passing information from one activity to a second activity.

Consider an Android calculator application page that has two text fields and a button to initiate addition. When a user fills both the text fields with numbers and clicks the button, a new page is opened that shows the result of the addition. This is an example of Explicit Intent in Android.

Another example of Explicit Intent in Android can be seen on applications that require users to enter their username and password. When a user is on the login page of an app, he enters his username and password correctly and submits this information. Doing so opens a new page in the same app that displays his profile, which has his username as well. This is an example of using Explicit Intent to pass information from one activity to another. In this example, the information is the username.

Important Methods of Explicit Intent in Android

Now that you know what the Explicit Intent tool is and how it works in Android applications, it is time for us to move towards learning how to code this tool into our applications.

Android app developers use many methods when it comes to implementing Explicit Intent in an application. Out of those methods, a few methods are very important and basic for the implementation of Explicit Intent. Here are the three most important methods used for Explicit Intent in Android.

Context.startActivity() This method is used for launching a new activity. It is also used for making an existing activity to perform the operation.

Context.startService() This method is used for starting a new service. Other than that, it is used for delivering instructions to an existence service.

Context.sendBroadcast() This method is used for delivering broadcast intent to broadcast receivers.

Explicit Intent in Android Steps

You must be thinking that a tool like Explicit Intent with such a complex way of working is very hard to implement in an Android application. Here is the truth: It’s not hard, especially not with our tutorial to help you. In this example, we will be showing you how to make use of Explicit Intent to call a new activity to be launched in response to a user action.

To start the process of coding to implement Explicit Intent in Android, you must open the Android Studio IDE (Integrated development Environment) first.

After opening Android Studio, create a new project. The detailed method of creating a new project is discussed in our earlier tutorials.

While you are creating a new project, select an appropriate name for your project. If you can’t come up with a project name, you can use the same project name as ours. We have simply used “Intent” as our project name.

You will also be asked to use a suitable package name for your project. You can take our package name, which is “com.androidaura.eintent,” as an example.

When you are done with creating a new project, open the MainActivity.java class file of your project in order to add the following code in it:

As you know that we have two screens or activities in our project, this code is for the first screen. With this code, we have defined the basic elements of our first screen and what they will do. For example, we have used the Button widget to give our screen a button, which will take the user to the second screen when used. This is done by using setOnClickListener() method to apply OnClick event on the button.

Now, open the layout file, i.e. activity_main.xml, of your project so that you can add the following code in it:

With this code, we have designed the UI (User Interface) of our first screen. For example, we have used this code to give the button of the first screen to have the text “Button 1” written on it. After coding in your activity_main.xml layout file, create a new java class file, name it “SecondActivity.java” and add the following code in it.

We have used this java code for our second screen. This code will simply open the layout of the second screen. In this file, the code says that we have given our second screen a button (by using the Button widget) and a message for the user (by using the Toast widget). The Toast message is coded to read “Hello From Activity One” and disappear after a short time.

Now, create a new XML layout file and name it “activity_second.xml” to add the following code in it:

Similar to our first XML layout file, we have used this file and code to design the UI but of the second screen this time. For example, with this code, we have designed the button of this screen to have the text “Button 2” written on it.

After this, create a new AndroidManifest.xml file. This file will contain the information of your package, like application components, such as services, broadcast receivers, activities, etc. In your AndroidManifest.xml file, enter the following code:

The most important thing to note and remember in this code is the placement of intent filter. Make sure you correctly add the intent filter right after the main activity.

After entering all the above-mentioned codes in their respective files, your project will be ready to be launched. After launching your project, you will be able to see the output of your coding. With our codes, your output would look like this:

Output

explicit intent

explicit intent