Android Tutorials for Beginners-2: Android Intent

Last updated on Feb 24,2022 12.5K Views

Android Tutorials for Beginners-2: Android Intent

edureka.co

 

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).

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

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:

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).

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:

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

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

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

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:

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:

BROWSE COURSES