What is Android Intent and Types of Intent

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 : 

You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.

what is Android Intent and example

  •  Activity A creates an Intent with an action description and passes it to startActivity().
  •  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 Intent.

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 onActivityResult() callback

2. Starting a service : 

With Intent, you can start service by passing an Intent to 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 Intent to bindService().

3. Delivering a broadcast

You can deliver a Broadcast to other apps or own android app by passing an Intent to sendBroadcast() or 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 flag  :

  • Action: A string that specifies the generic action to perform, such as #ACTION_VIEW#ACTION_EDIT#ACTION_MAIN, etc.
  • Category: Gives additional information about the action to execute. For example, #CATEGORY_LAUNCHER means 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 android.net.Uri.

In this code of AndroidManifest.xml file, let’s see its a main activity to launch have <action> and <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)
MinSdkVersion=”26″
TargetSdkVersion=”27″
Coding in Kotlin


One thought on “What is Android Intent and Types of Intent

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.