Lesson 22. Intent, Intent Filter, Context - Theory
Android Lessons

Lesson 22. Intent, Intent Filter, Context – Theory


In this lesson:

– Understand the code of Lesson 21
– Intent and Intent Filter theory
– a bit about Context

In the last lesson (# 21) we created application programWhich contains two Activity. Let me remind you that to create Activity, you need:
create a class, With superclass android.app.Activity
create an Activity record in the manifest and specify the created class in the Name field

I hope the last lesson did not cause any particular difficulties and the procedure for creating the Activity is approximately in the head. Now we can look at Activity code.

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

We used the object Intent. You can read about it here, here and here. True info is difficult enough to understand from scratch. I will try to explain in my own words.

What is Intent

In our case Intent is it objectIn which we are prescribed, which Activity we need cause. Then we pass this Intent object to the method startActivityFinder corresponding to Activity and shows it. We used a constructor when creating Intent Intent (Context packageContext, Class cls) with two parameters.

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

The second parameter is the name of the class. Recall that when creating an Activity record in the manifest file, we specify the class name. And now if we specify the same class in Intent – then the system, having looked through the manifest file will find out conformity and will show corresponding Activity.

You can easily see this. We will remove the Activity record from the manifest file and try to retrieve it afterwards. Open a project from the last lesson P0211_TwoActivity, open manifest file, tab Application and delete the entry for ActivityTwo using the button Remove. Save it all, launch the application and try calling the Activity with the “Go to Activity Two” button. The application will produce an error. If you look at the logs, we see the following text:

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

(The list is a LogCat tab in Eclipse. If not visible, go to Window -> Show View -> Other, Android folder -> LogCat)

The system tells us that it did not find such an Activity class and kindly suggests that maybe it not spelled out in manifest file. Type Activity again in the manifest file, save and run everything. It should work now.

a clear challenge

An Activity call using such an Intent is apparent challenge. That is, with the help of class obviously we specify which Activity we would like to see. This is commonly used within one program. This can be summarized as follows:

This is where we create Intent, As a parameter we pass it class Class_B. Next we call the method startActivity with created Intent as a parameter. method checks AndroidManifest for availability Activity related to the class Class_B and if it finds, it displays. All this within one program.

implicit call

there is also implicit Activity call. It is different when created Intent we use not a class, and fill in parameters action, given, category 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 specified number, etc. In turn for Activity we prescribe Intent Filter is a set of the same parameters: action, given, category (But the values ​​are their own – they depend on what the Activity can do). And if the parameters of our Intent coincide with the terms of this filter, the Activity is called. But the search is already underway on all Activity of all applications in the system. if located sprat, Then the system provides you choiceWhich program you want to use. This can be summarized as follows:

In Application_1 is created Intent, The parameters are populated action, given, category. For convenience, the set of parameters is called Param_C. Using startActivity this Intent goes in search suitable An activity that will be able to do what we need (ie what is defined using Param_C). There are different applications in the system, and in each of them several Activity. Intent Filter is defined for some Activity (Param_A, Param_B, etc.), some is not. the startActivity method verifies set of parameters Intent and parameter sets Intent Filter for each Activity. if the sets coincide (Param_C for both) then Activity is considered suitable.

If only one Activity is found as a result, it is displayed. If found sprat corresponding Activity, then the user is output list, Where he can choose which program to use.

For example, if you have multiple music players installed on your system and you are launching mp3, the system will show you a list of Activities that can play music and ask you which one to use. And those Activities that are able to Edit text, show pictures, ring, etc. will be ignored.

If an Intent Filter (Activity_24 on the schema) is not specified for Activity, then Intent with its parameters will not work in any way, and it will also be ignored.

If you make analogies – you can compare Intent with the key, and Intent Filter with the lock, which sits a wonderful Activity)))

We will gradually learn the nuances of this mechanism and value which populate the action, data and category parameters in Intent and Intent Filter. Now it is important to understand that implicit one application sends a call Intent, And everyone else checks its settings with theirs Activity -> Intent Filter. Intent is the basic concept of Android without us. It applies not only to Activity. But more on that later.

Well, I wanted to write a couple of introductory words, and a very detailed explanation came up with the diagrams and examples) I hope that I was able to convey the meaning of Intent technology. In the future, we will practice and consolidate the topic.

In the next lesson:

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




Discuss in the forum [9 replies]

Leave a Reply

Your email address will not be published. Required fields are marked *