Android Certification Training Course
- 65k Enrolled Learners
- Live Class
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.
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).
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:
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.
Intent Object contains all the information that the ‘Intent’ means to convey:
Delving further, Intents can be Explicit or Implicit.
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.
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.
Above implicit intent tells the Android System to view the Web Page provided in the URI.
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.
We will discuss more on Intent Filters in our later tutorials. Stay tuned for that. For now, you can join Android Course.