Intent Mechanism in Androidby@azamatnurkhojayev

Intent Mechanism in Android

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

The foundation of learning programming for Android is basic categories – the “bricks” from which we create a full-fledged app. We'll understand what it is and how to use it today. In the Android OS, many app mechanisms work based on the Intent. The Intent is a variable that describes a specific action. After specifying which Activity should be triggered, the code must pass this variable to the startActivity method. It automatically finds the desired Activity and shows it. The difference between explicit invocations and implicit invocations is explained.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Intent Mechanism in Android
Azamat HackerNoon profile picture

@azamatnurkhojayev

Azamat

Learn More
LEARN MORE ABOUT @AZAMATNURKHOJAYEV'S EXPERTISE AND PLACE ON THE INTERNET.

Credibility

react to story with heart

The foundation of learning programming for Android is basic categories. They are the framework of the entire source code – the “bricks” from which we create a full-fledged app. One of these categories is Intent. We'll understand what it is and how to use it today.

Intent

The Intent is a variable that describes a specific action. In the Android OS, many app mechanisms work based on this parameter.

Most often, we use Intent to launch another activity in the application. However, we can use the mechanism for other purposes. For example, to notify about the launch of a third-party Activity or another service that performs specific actions. Furthermore, Intent gets used to notify about a specific event.

In other words, the Intent is an object that specifies which Activity should be triggered. It looks something like this:

val intent: Intent = Intent(this, ActivityTwo::class.java)
startActivity(intent)

After specifying which Activity should be triggered, the code must pass this variable to the startActivity method. It automatically finds the desired Activity and shows it.

The Intent is created using two main components.

First of all, this is Context. Activity is a subclass of the Context variable. We can use it in the form of "this".

Context is a source code variable that gives access to the key functions of the application. These include access to resources, files, and so on.

Thanks to this parameter, the application can perform several Activity (Activities), which can be triggered by having access only to the class name.

The second component is the class name. It must be specified when adding an Activity to the manifest. By specifying this name in Intent, the code algorithms will automatically find the similarity and launch the desired Activity.

It's easy to check. You only need to remove the Activity name from the manifest and try to activate it. After these actions, the application will start generating error alerts. The log will indicate that such a class does not exist. Also, the system itself will tell us it's not written in the manifest. There won't be any errors if you rewrite the Activity in the manifest.

Explicit Invocation

An explicit Activity invocation is an invocation using the Intent parameter in which you entered the class name. This method is used to work within a single program. It looks something like this:

image

We create an Intent variable in the parameter of which Activity – Class_B is written. After that, we start the startActivity method with the added Intent. The program automatically finds an Activity with the Class_B parameter in the manifest file. If such a variable does not exist, we'll see an error. Otherwise, everything works. The whole process takes place inside one program.

Implicit Invocation

An implicit call differs from an explicit one. In this case, we don't specify the class name in the Intent. Instead, we use the parameters action, data, and category. They all have a specific meaning. Added together, these values create a task for the code.

In the Activity variable, we specify the IntentFilter parameters. They also include action, data, and category. The values of these variables are determined by the specifics and tasks of the Activity.

If these parameters are consistent with Intent, the program starts working (that is, it invokes Activity).

The difference from an explicit invocation here is that such a method will work within both one application and the entire OS. It means an implicit invocation causes the code to search for an Activity not only inside one program but also throughout the device's files.

To illustrate the principle of an implicit invocation's operation, we can do the following:

image

The Intent parameter is created inside the first application (Application 1). There, we prescribe the categories of action, data, and category. Then, using startActivity, we start the search for the desired Activity.

The program starts searching for Activity in all the system's applications – this is illustrated using different applications (Application) in the picture.

Inside the system, StartActivity is searching for an application with an Activity that has the necessary parameters. It means it's looking for a variable that performs the function specified in the Intent categories.

There may be several suitable Activity (Activities). If there is more than one of them, the user opens a list of applications from which they can select the needed program.

Hence, the difference between explicit and implicit invocations is that we don't use the class name in the second case – three other parameters do the work instead. Their particularity allows the program to work outside the limits of a single application.

Methods for Working With Intent

There are three main methods for working with Intent parameters:

  • getCategories – receives all categories according to the criteria set in the function parameters;

  • removeCategory – removes categories;

  • addCategory – adds a category to the Intent.

We use them to work with categories in the code.

IntentFilters

We need filters for Intent so that the system can automatically select one application from the list of possible ones that will perform the desired function. Filters select a list of Activities and other parameters that can work with a specific data type.

We can create a code element that will act as an Intent filter by adding the <intent-filter> tag to the manifest.

The filter specifies the Intent–action, data, and category components. Other elements do not affect the operation of the filter.

The ready-made filter looks like the following in the example of the "HelloWorld" application:

<activity android:name=".HelloWorldActivity"
          android::label="@string/app_name">
    <intent-filter>
          <action android:name="android.intent.action.MAIN" />

          <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The filter in the Action element registers the Activity that will be launched by default.

Filters for Intent work according to a specific principle.

During the launching, StartActivity leads Intent to only one Activity. As mentioned before, if several variables are suitable for performing a function, the user can select the needed one from the list.

The system collects all filters from the scanned applications. Filters that do not correspond to the task get removed from the list.

If none of the tasks from the list specified in Intent matches the ones specified in the filter, there will be no match. Besides that, the Intent filter must contain all the categories specified in the Intent.

The URI from Intent gets automatically compared with the data tag in the Intent filter. If there is no match, the filter gets removed from the list.

In new versions of the Android OS, the Intent filters' algorithms have changed. If you operate following the old principles, the code will not work.

The main innovation concerns the android:exported parameter. It has four elements that can be true or false.

Don't set true on all lines. We use this parameter for the launcher block for the application to start. We also apply it if the Activity uses android.intent.action.view. It's needed so that we can run the program using other applications.

Examples of Using Implicit Intent

We use Intent almost everywhere. For example, here is a piece of code whose task is to open any URL automatically:

val linkAddress: Uri = Uri.parse("https://developer.android.com")
val openLink = Intent(Intent.ACTION_VIEW, linkAddress)
startActivity(openLink)

We specify the link in quotation marks and brackets after the Uri.parse.

Also, we can force Android to open a search query in the browser for the word "Android":

val text = "Android"
val intent = Intent(Intent.ACTION_WEB_SEARCH)
intent.putExtra(SearchManager.QUERY, text)
if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent)
}

There are a lot of such examples. We can use Intent to work with the device's camera, microphone, audio files, system settings, and almost all elements of the Android OS.

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa