You get a bonus - 1 coin for daily activity. Now you have 1 coin

22. Intent, Intent Filter, Context - theory

Lecture



In this lesson:

- we understand the code of lesson 21
- theory of Intent and Intent Filter
- a little about Context

In the last lesson (No. 21) we created an application that contains two Activities . Let me remind you that in order to create an Activity, you need:
- create a class with the superclass android.app.Activity
- create an Activity record in the manifest and indicate to it the created class in the Name field

I hope the last lesson did not cause any particular difficulties, and the procedure for creating an Activity was about the same time in my head. Now we can pay attention to the code calling the Activity .

Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);

We used the Intent object. About him you can read here, here and here. True infa is quite difficult to understand from scratch. I will try to explain in my own words.

What is Intent

In our case, an Intent is an object in which we prescribe what activity we need to trigger . After that we pass this Intent-object to the startActivity method, which finds the corresponding Activity and displays it. When creating the Intent, we used the Intent constructor (Context packageContext, Class cls) with two parameters.

The first parameter is Context. If you remember, when we programmatically created View in one of the past lessons, we also used the Context object in the constructors. Activity is a subclass of Context , so we can use it - this . In short, Context is an object that provides access to the basic functions of an application such as: access to resources, the file system, the Activiy call, etc. I think in the future we will look at examples where we will clearly see why the Context is transmitted and how it is used.

The second parameter is the class name . Recall that when creating the Activity record in the manifest file, we specify the class name. And now if we specify the same class in the Intent, then the system, by viewing the manifest file, finds a match and shows the corresponding Activity.

This can be easily seen. We will delete the Activity record from the manifest file and try to call it after that. Open the project from the previous lesson P0211_TwoActivity , open the manifest file , the Application tab, and remove the entry about ActivityTwo using the Remove button. Save all, launch the application and try to call the Activity button “Go to Activity Two”. The application will generate an error. If you look at the logs, we see the following text:

ERROR / AndroidRuntime (367): android.content.ActivityNotFoundException: Unable to find explicit activity {ru.startandroid.develop.p0211twoactivity / ru.startandroid.develop.p0211twoactivity.ActivityTwo}; have your AndroidManifest.xml?

(Logs - the LogCat tab in Eclipse. If you don’t see this, then go to the Window menu -> Show View -> Other, the Android folder -> LogCat)

The system tells us that it did not find such an Activity class and kindly prompts that, perhaps, it is not registered in the manifest file . Record Activity again in the manifest file, save and run everything. Now it should work.

Explicit call

Calling an Activity with this Intent is an explicit call. Those. With the help of the class, we explicitly indicate which Activity we would like to see. This is commonly used within a single application . Schematically, this can be represented as:

  22. Intent, Intent Filter, Context - theory

Here we create an Intent , and pass the class Class_B as a parameter. Next, call the startActivity method with the created Intent as a parameter. The method checks AndroidManifest for the presence of an Activity associated with the class Class_B and if it finds it, it displays it. All this within the same application.

Implicit call

There is also an implicit call to the Activity. It differs in that when creating an Intent, we do not use a class , but fill in the action , data , category parameters with certain values. The combination of these values ​​determine the goal we want to achieve. For example: sending a letter, opening a hyperlink, editing text, viewing a picture, calling a specific number, etc. In turn, we assign an Intent Filter for the Activity - this is a set of the same parameters: action , data , category (but the values ​​are already their own - they depend on what the Activity can do). And if the parameters of our Intent match the conditions of this filter, then the Activity is invoked. But at the same time the search is already going on all the activities of all applications in the system. If there are several , the system gives you the choice of which program you want to use. Schematically, this can be represented as:

  22. Intent, Intent Filter, Context - theory

In Application_1 an Intent is created, the action , data , category parameters are filled. For convenience, the resulting set of parameters is called Param_C . With startActivity, this Intent is sent to look for a suitable Activity that can do what we need (that is, what is defined with the help of Param_C). The system has different applications, and each of them has several Activities. For some Activities, an Intent Filter is defined (Param_A, Param_B, etc.), for some it is not. The startActivity method compares the set of Intent parameters and sets of Intent Filter parameters for each Activity . If the sets match (Param_C for both), then the Activity is considered appropriate .

If in the end there was only one Activity - it is displayed. If there are several suitable activities, then the user is shown a list where he can choose which application to use.

For example, if several music players are installed in the system, and you launch mp3, the system will display a list of Activities that can play music and ask you to choose which one to use. And those Activities that can edit the text, show pictures, call, etc. will be ignored.

If an Activity Filter is not set for an Activity (Activity_24 in the diagram), then an Intent with parameters will not work for it, and it will also be ignored.

If you draw analogies - you can compare the Intent with the key, and the Intent Filter with the lock, behind which sits a wonderful Activity)))

We will gradually recognize the nuances of this mechanism and the values that fill in the action, data and category parameters in the Intent and Intent Filter. Now it is important to understand that in the case of an implicit call, one application sends an Intent , and all others compare its parameters with their Activity -> Intent Filter . Intent is the basic concept of the Android system and without it, we have nowhere. It applies not only to the Activity. But more about that later.

Well, I wanted to write a couple of introductory words, but I got a rather detailed explanation with diagrams and examples) I hope that I managed to convey the meaning of the technology Intent. In the future we will practice and fix the topic.

In the next lesson:

- Activity LifeCycle - Activity behavior when creating, calling, closing


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)