In you android programming road map it’s inevitable to skip this topic. So, let us look at what is intent in Android ?
Quick Navigation
What is Intent in Android ?
Intents in simple and non technical terms is the intention to do something. Intents facilitate the communication between components.
In other words Intents help an application to perform tasks. And by tasks, I mean actions such as:
- Starting another activity
- Starting a foreground or background
- Sending data back and forth between unrelated components, broadcast receivers etc
Let us look at a sample usage of Intent to launch an Activity.
val intent = Intent(this, TargetedActivity::class.java)
context.startActivity(intent)
Code language: Kotlin (kotlin)
In the above example the code will start a new Activity
named TargetedActivity
.
Types of Intent
There are two types of intent in Android. They are Implicit Intents and Explicit Intents.
Let us look at each of the Intent in detail below.
What is Implicit Intent ?
Implicit by its definition is something that is not definite. In other words the there is no one answer to how things will happen.
And the implicit intent is something similar. The component is not known for an implicit intent. And there may be multiple available options at any given time.
Example for such intent is opening a image picker, sharing data with other application, receiving broadcast, etc.
There might be multiple component to handle such information. And the process involved is known as intent resolution. It is done with the help of intent filters.
Look at the code below:
val intent:Intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
Are you new to Android Programming ? If yes then you should really get familiar with Views.
What is Explicit Intent ?
Now that we have seen Implicit Intent. Let us understand the other type of intent: Explicit Intent.
The explicit intent is very different from the implicit as it requires a component. The developer must define the intent filters in manifest. For the Android system to resolve the intent.
A real world use case of explicit intent is starting an Activity. Example code:
val intent = Intent(this, TargetedActivity::class.java)
context.startActivity(intent)
Code language: Kotlin (kotlin)
In the code above the intent has components defined: TargetedActivity
. Meaning the system will only work on the specific case to accomplish the task.
We will be looking at Intent pretty much every time we do something in our application. I have purposely not added any other code here because without any contextual meaning the code will be of no use.
Explore the topics on this website to get more information about Android Intent.
If you want to learn more about Intent you can find the official documentation here.
Also, before you leave read how you can use retrofit for API in Android.