Today, we will take a closer look at ‘Intent’, a very important concept in Android app development. Intents are responsible for interactions between app components and are used to start other Activities or to call services, broadcast receivers, etc.
1. What is Intent?
An Intent is a message object that manages interactions between the components of an application. For instance, when a user wants to move to another Activity by clicking a button, an Intent is used. Intents are divided into two main types.
- Explicit Intent: An intent that specifies a particular component to start. It is generally used to call another Activity within the same application.
- Implicit Intent: An intent that does not specify a particular component but requests the system to find an appropriate component. This allows various applications or services to be called.
2. Components of Intent
An Intent consists of the following main components.
- Action: Defines the action to be performed. For example,
Intent.ACTION_VIEW
means the user wants to view a URL. - Data: Includes the URI of the data to be passed. For example, it could be a link to a specific webpage.
- Category: Describes what kind of component the intent will invoke. For example,
Intent.CATEGORY_DEFAULT
is a category for general intents. - Component Name: The explicit name of the component to be called. It includes the package name and class name.
- Extras: Key-value pairs for passing additional data.
3. Using Explicit Intents
To use an explicit intent, you must specify the class name of the Activity to be called. Below is a simple example code.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById
The above code creates an explicit intent that navigates to SecondActivity
when the button is clicked. It executes the intent by calling the startActivity(intent)
method.
4. Using Implicit Intents
An implicit intent allows you to request actions that can be handled by other applications. For instance, you can open a webpage or invoke a camera app. Below is an example of opening a webpage.
val openWebPageIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"))
startActivity(openWebPageIntent)
The above code opens the default web browser and navigates to https://www.example.com
when clicked by the user. To use an implicit intent, include the action of the intent along with the data URI.
5. Intent Filter
An intent filter defines what components can match with an implicit intent. Intent filters are defined within the manifest file and specify what kind of intents an activity can handle.
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http" android:host="www.example.com"/>
</intent-filter>
</activity>
The above code sets up SecondActivity
to handle links to http://www.example.com
. Now, when a user sends an intent to open that site, SecondActivity
will be invoked.
6. Passing Data with Intent
You can pass data (e.g., strings, numbers, etc.) to another Activity through an Intent. Below is an example showing how to pass data.
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("EXTRA_MESSAGE", "Hello from MainActivity")
startActivity(intent)
The above code passes a string using the key EXTRA_MESSAGE
. Here is how to receive this data in SecondActivity
.
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val message = intent.getStringExtra("EXTRA_MESSAGE")
val textView = findViewById(R.id.textView)
textView.text = message
}
}
The above code is an example of receiving a message sent from MainActivity
in SecondActivity
. You can use the getStringExtra()
method of the Intent object to receive the data.
7. Setting Intent Behavior with Flags
Intent flags control the behavior when executing an intent. For example, if you want to destroy existing activities when starting a new Activity, you can use the FLAG_ACTIVITY_NEW_TASK
flag. Below is an example of setting flags.
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
The above code sets the behavior to delete all previous Activities while moving to MainActivity
.
8. Returning Results: StartActivityForResult
If you want to start an Activity and receive a result back from it, you need to use the startActivityForResult()
method. Below is a simple example.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val result = data?.getStringExtra("RESULT_DATA")
// Process the result
}
}
This method allows you to handle the result when called by the invoked Activity. Here, REQUEST_CODE
is a constant defined to distinguish which request it is.
9. Conclusion
In this tutorial, we covered various topics ranging from the basic concept of intents, usage of explicit and implicit intents, intent filters, data passing, use of flags, and result handling. Intents are essential elements in designing Android apps, and I hope this tutorial enables you to further explore app development using various intents.
10. Additional Resources
Below are links where you can find more information about intents:
Thus concludes the Android app development tutorial using Kotlin, Understanding Intents. If you have any questions or comments, please leave them in the comments!