Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Android, an Intent
is a messaging object you can use to request an action from another app component. It acts as a mechanism to abstract an operation to be performed, serving as a bridge between separate components (such as activities, services, or broadcast receivers).
Explicit Intents: These specify the component to start by name (i.e., the fully-qualified class name). They are typically used for application-internal operations.
Intent intent = new Intent(this, TargetActivity.class); startActivity(intent);
Implicit Intents: These do not name a specific component. Instead, they declare a general action to perform, allowing a component from another app to handle them.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com")); startActivity(intent);
In the example above, if a web browser is available on the device, it will be launched to open the given URL.
Starting Activities: Using startActivity()
, you can launch a new activity.
Starting Services: With startService()
, you can start a background service.
Delivering Broadcasts: Broadcasts can be sent with sendBroadcast()
, sendOrderedBroadcast()
, or sendStickyBroadcast()
.
Data Transfer: Intents can carry data payloads of various types within an Extras
bundle. For example, you might send integer data with intent.putExtra("key", value)
.
Task Management: Intents can be used with the task and back stack manager to create new task stacks or bring existing tasks to the foreground.
Common Actions: There are predefined actions in the Intent
class that allow for common operations, such as ACTION_VIEW
, ACTION_EDIT
, ACTION_MAIN
, and others.
Integration with Other Apps: For instance, if you want to show a location on a map, you can use an intent to request that another capable app show that location.
For implicit intents, Android determines the appropriate component (e.g., which app or activity should handle the intent) by comparing the intent to intent filters defined in the manifest file of other apps on the device.
An IntentFilter
specifies the types of intents that a component can handle. It describes a combination of the following:
ACTION_VIEW
, ACTION_EDIT
, etc.If the intent matches the criteria defined in the IntentFilter
, that component becomes a candidate to respond to the intent.
In conclusion, Intent
in Android is a fundamental concept that provides a powerful way to perform late runtime binding between components, facilitating flexible application architectures and integration points between apps.
Using Intent for Activity Navigation in Android:
Intent
to navigate between activities.Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); startActivity(intent);
Passing Data Between Activities with Intent in Android:
putExtra()
to attach data to an intent.Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); intent.putExtra("key", "value"); startActivity(intent);
getIntent().getExtras()
.Launching External Activities with Intent in Android:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")); startActivity(browserIntent);
Intent Flags and Their Uses in Android:
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent);
Intent Services and Background Tasks in Android:
IntentService
is a subclass of Service
for handling asynchronous tasks off the main thread.Intent intent = new Intent(CurrentActivity.this, MyIntentService.class); startService(intent);
Customizing Intent Actions and Categories in Android:
<intent-filter> <action android:name="com.example.custom.ACTION"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter>