Android Intent: Intent is giving facilities to communicate between Android components in several ways. Android Intent is mostly using for launching new Activity form Activity. Its a glue between activity because of its join navigation from one to another activity. You can use intent for sending data between activities.
Android intent is the main Component for an android application, which means without it mostly app can build. Here are some uses of it to Starting an activity
startActivity ,Delivering a broadcast
broadcastIntent to send it to any interested
BroadcastReceiver components, and Starting a service.
Main fundamental use cases of Android Intent :
1. Starting an activity :
- Activity A creates an
Intentwith an action description and passes it to
- The Android System searches all application for an intent filter and that matches the intent. When a match is found,
- the system starts the matching activity (Activity B) by invoking its
onCreate()method and passing it the
If you want to receive a result from the activity when it finishes, call
startActivityForResult(). Your activity receives the result as a separate
Intent object in your activity’s
2. Starting a service :
With Intent, you can start service by passing an
startService(). It will one-time operation to do like downloading a file.
And If the service is designed with a client-server interface, you can bind to the service from another component by passing an
3. Delivering a broadcast
You can deliver a Broadcast to other apps or own android app by passing an Intent to
sendOrderedBroadcast(). The broadcast is a message and where receiver is receiving it.
Types of Intent :
There are types of Intents
1. Explicit intents: in this Intent, we know where an exact component or landing component. For example startActivity, we know about which activity will start.
Basically, you used this Intent for own application because you knew about where and which component related to each other.
Another example starts service to download the file.
2. Implicit intents: in this intent don’t have exact knowledge about landing component. It can be open another app or own app component or maybe there many options to open. Example You downloaded PDF, song, images etc any document now you want open it. In this case, every mobile has so many apps to open it. So what we do, we just define the type of file in intent and start intent. Android system will figure out which type of apps have the same IntentFilter to handle such action or files.
Android Intent Structure
For Building an Intent there is primary thing have to mention in
<intent-filter> or use
- Action: A string that specifies the generic action to perform, such as
- Category: Gives additional information about the action to execute. For example,
#CATEGORY_LAUNCHERmeans it should appear in the Launcher as a top-level application.
- Data: The data to operate on, such as a personal record in the contacts database, expressed as a
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
In this code of AndroidManifest.xml file, let’s see its a main activity to launch have
<category> so this attribute (elements) are describe the functionality of Intent.
- Component name: Specifies an explicit name of a component class to use for the intent.
- Type: Specifies an explicit type (a MIME type) of the intent data.
- Extras: This can be used to provide extended information or data to the component. For example, if we have an action to send an e-mail message, then we could also include extra data here to supply a subject, body, etc.
You can also pass data between activity using putExtra() and getExtra() for String, Int, boolean etc data, during the transaction of activities or other components.
Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)
Coding in Kotlin