Android Development (41 Blogs)

Android Tutorials for Beginners-2: Android Intent

Last updated on Feb 24,2022 12.7K Views


 

In our last Android Tutorial, we discussed the Activity component of Android application. This Android tutorial here covers the basics of the next building block, i.e. Android Intent.

If you want to get a live lecture covering the basics of Android development for free, sign up here!

Watch this video to get an overview of Intent.

1. Android Tutorials for beginners: Intent

In our Android tutorial covering fresher interview questions, we had explained the usage of Intent in Facebook with the following example:

Suppose you are on the news feed screen (which is one Activity), and want to view a pic posted by our friend. When you click on the photo, the intent associated with the click event of photo is fired which communicates the message, and the Photo page opens (which is a new Activity).

Android Tutorial: Intent ComponentSo, we assume that you understand a little about Android Intent. This Android tutorial would discuss a few deeper concepts associated with the topic.

  • Think of intent as a message to communicate an action. It is a description of what you want done, example: VIEW VIDEO, PLAY GAME etc.
  • They are commands which when called would act as communicators between the three core components of Android, i.e. Activities, Services and Broadcast Receivers.
  • While you are interacting with one activity, you might want to switch to another one; this is done by defining a proper Intent for the action. Here one Activity uses Intent to request the launch of another Activity. Thus, it is evident that using intents, one Android component can request action from the other components of Android.

To simplify this, let us once again take a similar example of the Facebook app.

The example below assumes that you are on the Photo Gallery Activity, and want to view a specific photo (which would open in its own activity). This is how the Intent associated with the click photo event is fired:

Android Tutorials for beginners: Intent component of Android

Sometimes, one can define Intent to notify the Android system about the occurrence of an event. This is precisely what happens in case of Broadcast Receivers (for instance, intent is defined to communicate battery low notification to the user). 

Let’s take another example of the Domino’s Pizza app. Let us suppose you are viewing the Menu Screen (Activity) of the application, and want to select a pizza. When you select the Mexican Wave pizza, and click on Customize button, another screen (activity) pops up, where you can further specify the size of the pizza, type of crust you want etc. Here, the Click Action (Select Pizza Intent) on the Menu communicated the message to open the new popup window (new activity).

Android Tutorials for beginners: Intent Example using Domino's Pizza app

Simple; right! Let us get a little into the details in the next part of the Android tutorial:

2. Intent Object

If you wish to make a dinner date with your friend, and are conveying the information through a messenger, you ought to give the messenger details like the name of your friend and the specifics of the desired meeting (time, place, etc.). Not having given the specifics of the meeting to the messenger, you cannot just assume that they would figure out what to do. You might write all this information on a piece of paper for the messenger’s reference.

Similarly, as intent is a message, the complete information that it wants to convey must be stored somewhere, right! Intent Object takes care of that.

2.1 What information does Intent Object contain?

Intent Object contains all the information that the ‘Intent’ means to convey:

  1. All the information required by the component receiving the Intent (for instance, the action to take on receiving the Intent). (In the above example, this would be the information meant for your friend’s knowledge, like the location of the meeting, time    of the meeting etc.)
  2. The information required by the Android system (for instance, the description of the Component category the Intent is directed at) is contained in the Intent Object(In the above example, the information required by the messenger itself would form a part of this, like the name of the friend, his address etc.)

The following information forms a part of Intent Object:

  • Component name: This information is optional. In case the component name is set, the Android system directly maps the intent to the target component. In other cases, the system utilizes other information to locate the suitable target. The component name is set by setComponent (), setClass () or setClassName (), and is read by getComponent ().
  • Action: This piece of information names the action an Android component should take on receiving the Intent, or the action that has already taken place, and is being notified to the system. The Android system defines a number of Action Constants. We have listed two in this Android tutorial:

Android Tutorials: Intent Object Action

  • Data: Contains the Uniform Resource Identifier (URI) of the data. There are different types of data specifications for different actions. For instance, if the action defined were ACTION_CALL, the data field would contain a number to call (tel: URI). For correctly matching Intent to a suitable component, defining the data type (in addition to the URI) helps. For instance, a component to display image should be called to display image only, not to play an audio file.
  • Category: This defines the category of the component that would handle the Intent.
  • Extras: This is the additional information that needs delivered to the component handling the intent.
  • Flags:  Many flags can be a part of the information contained in Intent Object. For instance, flags can instruct the Android system to launch an activity in a specific manner (you can specify that the new activity be launched in a new window of the browser.)

3. Intent Types

Delving further, Intents can be Explicit or Implicit.

3.1 Explicit Intent:

An Explicit Intent is exactly what it means literally: an “explicit intention” to perform an action. Simply put, in this case, we define explicitly the Android component (Activity, Service or Broadcast Receiver) that needs calling.

Sample Code 1: Explicit Intent sample code Android Tutorials: Explicit Intent sample code Android Tutorials for beginners: Explicit Intent Example

3.2 Implicit Intent

Here we do not specifically define the component that needs to be called. However, the Intent contains enough information to direct the system to fetch the correct one.

Sample Code 2: Implicit Intent sample code Android tutorials for beginners: Implicit Intent sample code

 Above implicit intent tells the Android System to view the Web Page provided in the URI.

Implicit Intent Example: Basic Android tutorials

4. Intent Resolution

Because of the arbitrary nature of Implicit Intents, the system uses a process called Intent Resolution to map them correctly. The system does this by matching the Intent description with the default descriptions in the system.

Intent Object, defined earlier comes into play here. The Intent Resolution compares the contents of Intent Object against the Intent Filters. (Intent Filters are associated with the different Android components, and can receive Intent.  Intent filter is a way for Android components to declare their capabilities to the Android system.)

Filters play an important role in defining the kind of intent an Android component can receive. A component with no filters can receive only Explicit Intents, whereas components with filters are capable of receiving both Explicit and Implicit Intents. The Intent Resolution uses the following information to map the Intent to the appropriate Android component:

  • The action
  • The type (data type and URI)
  • The category

As can be seen, extras and flags have no role here.

Having gone though this Android tutorial, you probably now understand the concepts of Android Intent a little better.

Have a doubt with any topic discussed in this Android tutorial? Ask Us!

We will discuss more on Intent Filters in our later tutorials. Stay tuned for that. For now, you can join Android Course.

Happy Learning!

You may also like these related posts:

Comments
31 Comments
  • This guide helps me a lot in understanding the basics of developing android app! Thanks a lot and stay awesome! ;)

  • hello there and thank you for your info – I’ve certainly picked up anything new from right here. I did however expertise some technical issues using this website, as I experienced to reload the web site many times previous to I could get it to load correctly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for much more of your respective intriguing content. Ensure that you update this again very soon..

  • This will be a fantastic website, would you be interested in doing an interview about how you designed it? If so e-mail me!

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Android Tutorials for Beginners-2: Android Intent

edureka.co