Tutorial details

Lesson 22. Intent, Intent Filter, Context - theory | App Code for Sale | Preview

Lesson 22. Intent, Intent Filter, Context - theory | Android Tutorial

How to work with Intent and Intent Filter

Overview PAGE TOP

In this lesson:

  • we look throught the code of lesson 21
  • theory about Intent and Intent Filter
  • a little about Context

In the previous lesson (№21) we have created an application that consists of two Activities. I will remind you that to create an Activity you need to:

  • create a class that extends android.app.Activity
  • create an Activity-tag in the manifest-file and specify the class created for it in the Name field.

    I hope the previous lesson wasn’t too complicated and you are now comfortable with Activity creation procedure. Now we can pay attention to Activity invocation code.

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

We’ve used an Intent object. You can read about it here, here and here. But the information is quite complicated for understanding from scratch. I will try to explain in my own words.

What Intent is PAGE TOP

In our case Intent is an object in which we specify, which Activity we need to invoke. After it we pass this Intent-object to startActivity method, which finds the corresponding Activity and shows it. When creating an Intent we have used Intent(Context packageContext, Class cls) constructor with two parameters.

The first parameter is Context. If you remember, when we created View in one of our previous lessons, we have also used a Context object. Activity is a subclass of Context, that’s why we can use Activity as a Context object - this. To be short, Context is an object that grants access to basic application functions such as: access to resources, file system, Activity invocation, etc. I guess in future we will explore examples where we will see explicitly what Context is used for and how it is used.

The second parameter is a class name. I will remind that when creating Activity, we specify its class name in the manifest-file. Now if we specify the same class for Intent - the system will look up the corresponding Activity in the manifest-file and show it.

You can check it easily. We will delete the record about the Activity from the manifest-file and will try to invoke it afterwards. Open the project from previous lesson P0211_TwoActivity, open the manifest-file, Application tab and delete the record about ActivityTwo using the Remove button. Save everything, run the application and try to invoke Activity using "Go to Activity Two" button. An error will occur. If you check the log, we can 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?

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

System tells us, that it not found such Activity class and kindly hints us that it may be not declared in the manifest file. Register Activity in the manifest-file again, save everything and run the application. It must work now.

Explicit invocation PAGE TOP

Invoking Activity with such an Intent is an explicit invocation. That is, using the class we explicitly specify which Activity we would like to see. It is usually used inside one application. It can be illustrated in the following way:

TAH.jpg

We create an Intent here and pass it ClassB class as a parameter. After this we invoke startActivity method with created Intent as a parameter. Method checks if Activity (with ClassB) is present in AndroidManifest, if yes it displays the Activity. It is all in the bounds of single application.

Implicit invocation PAGE TOP

There is also an implicit Activity invocation. The difference is that for creating an Intent we use not a class object, but fill action, data, category parameters with specific values. The combination of these values defines a goal which we are trying to accomplish. For example, sending a letter, opening a link, editing some text, viewing an image, calling a specific number and so on. By-turn we specify an Intent Filter for the Activity - it is a set of the same parameters: action, data, category (but the values are its own, they depend on what Activity is capable of doing). And if the parameters of our intent match the conditions of this filter, the activity is invoked. But now the search goes through all the Activities in the system. If several Activities are found, the system gives you a choice, which application exactly would you like to use. It can be illustrated as following:

TAH286.jpg

Intent is created in Application1, action, data, category parameters are filled. Let’s name this set of parameters ParamC for convenience. Using startActivity, this Intent will be sent to look for an appropriate Activity, which will be able to accomplish what we need (that is what is defined by ParamC). There are different applications in the system, and each of them has several Activities. For some of them an Intent Filter is defined (ParamA, ParamB sets and so on), for some it is not. startActivity method matches the set of parameters from Intent and sets of parameters of Intent Filter for each Activity. If the sets match (ParamC) for both, the Activity is considered appropriate.

If, in the end, only one Activity is found - it is displayed. If there are several Activities found, the user is given a list, from which he can choose which application to use.

For example, if there are several music players installed on the system and you open an mp3 file, the system will show you the list of Activities, that are able to play music and will ask you to choose which one of them to use. And those Activities that can edit text, show pictures, make calls and so on, will be ignored.

If there is no Intent Filter for an Activity (Activity_24 on the picture), in this case Intent with parameters will not fit it in any way and the Activity will also be ignored.

If trying to find an analogy - Intent can be compared to a key and Intent Filter to a lock, which hides our awesome Activity )))

We will gradually find out nuances of this mechanism and values, using which you can fill action, data and category in Intent and Intent Filter. For now, it is important to understand, that in case of implicit invocation, one application sends an Intent and all other applications check this Intents’ parameters with their own Activity -> Intent Filter. Intent is a basic concept of Android system and you can not do without it. It is used not only for Activity. But we will talk about it later.

This is it, I wanted to write a few introductory words but came up with quite detailed explanation with illustrations and examples ) Hope I’ve managed to deliver the idea of Intent concept. In further lessons we will have some practice and make our knowledge more solid.

Reference PAGE TOP

http://startandroid.ru/en/lessons/complete-list/229-lesson-22-intent-intent-filter-context-theory.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.