Go back

Intents and Intent Filters in Android

Published on 9 May, 2025

You opened Chrome, you saw a cool photo and you want to share it with your friend. You long press that image and press share. Immediately you see a bunch of apps with which you can share that image. Let’s say you select whatsapp and immediately, whatsapp opens with that image selected, ready to be shared with anyone.

In other scenario, you downloaded a song, you open your file manager and select the song and your file manager goes to another screen and stars playing the song.

Ever wondered, how apps share data with other apps or perhaps with itself?

Well that’s Intents and Intent Filters in action.

What is an Intent

An Intent (short for Intention), is an action that tells about you want to achieve with that data. In other words, an Intent is a messaging object you can use to request an action from another app component.

Intents usually have three fundamental use cases:

  1. Starting an activity : An Activity represents a single screen that a user sees and interacts with. Intents can be used to start a new instance of the Activity and also provides any necessary data required for some operation.

  2. Starting a Service: A Service is the code that runs in the background without an user interface. Intents can be used to start a new service.

  3. Delivering Broadcast Messages: Broadcast messages are events that are sent to entire OS. Intents can be used to broadcasrs events to other apps.

Types of Intents

  1. Explicit Intent: Specifies the exact component such as an Activity to be opened in the app.

    // Explicit Intent for going to another activity.
    @Composable
    fun NavigateButton(context: Context) {
        Button(onClick = {
            Intent(context, SecondActivity::class.java){
                startActivity(it)
            }
            
        }) {
            Text("Go to Second Activity")
        }
    }
  2. Implicit Intent: Declares a general action to perform, allowing other apps to handle it if they can.

    // Implicit Intent for opening youtube app.
    @Composable
    fun OpenCameraApp() {
        Button(onClick = {
            Intent(Intent.ACTION_MAIN)).also{
                it.`package` = "com.android.camera2"
                startActivity(it)
            }
        }) {
            Text("Open Camera")
        }
    }
  3. In AndroidManifest.xml, do add <queries>, so that the resolveActivity function can work.

    <queries>
            <intent>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent>
        </queries>

Passing Data in Intents

We can also pass data in the intents. Here’s an example where we are trying to send an email from some another app using our app.

val intent = Intent(Intent.ACTION_SEND).also{
    type = "text/plain" // type of data: plain text
    putExtra(Intent.EXTRA_EMAIL, arrayOf("test@test.com"))
    putExtra(Intent.EXTRA_SUBJECT, "Knock Knock?")
    putEXTRA(Intent.EMAIL_TEXT, "Never Gonna Give You Up")
}
// we need to check that there exist an app (email) that can handle it before sending intent
if(intent.resolveActivity(packageManager) != null)}{
    startActivity(intent)
}

Now in above example, there’s a small warning you can see with resolveActivity. This is a security mechanism to prevent one app to scan for other apps. Basically, if you want your app to look for other apps you need to explicitely state that in AndroidManifest.xml file. I will make a post in future about this manifest file in detail. But right now to resolve this follow the steps below.

<queries>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimetype="text/plain" />
    </intent>
</queries>

Now, Let’s say you built an app which can play music and user installed your app on their system. Now they download a song from chrome and clicks on the song. A chooser with different apps shows up which can play that song. However your’s dont. Wonder why?

That’s because your app needs to tell the android operating system, which intents it can handle. Hence, we use Intent Filters.

Intent Filters

Intent Filters tells android os which intents they can handle. In other words, an app tells android os that it can work with images, songs etc

To declare intent filters, first we need to add intent-filter in manifest.xml file.

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:label="intentsAPP"
    <!-- single instance will only open one activity -->
    android:launchMode="singleInstance"
 >
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*">
</intent-filter>
</activity>

Now, if you share any image from any app, you will see this app inside the chooser. However we don’t see any image yet, because we need some code to get the image data, parse it and then show it inside our app.

override fun onNewIntent(intent:Intent?){
    super.onNewIntent(intent)
    val uri = if(Build.VERSION.SDK_INT >= BUILD.VERSION_CODES.TIRAMISU){
        intent?.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
    }else{
        intent?.getParcelableExtra(Intent.EXTRA_STREAM)
    }
    // we can use a viewmodel to update URI, so that composables using this viewModel can be updated.
    imageViewModel.changeImageURI(uri)
}

Since, we have got intent in singleTop mode, hence we need to override this onNewIntent() inside our MainActivity which can receive these new Intents.

Passing Data from one Activity to another Activity

Assuming we have two activities in our application and let’s say we want to pass something like an username from one activity to another we can use putExtra() and then move to second activity.

Intent(this@HomeActivity,ProfileActivity::class.java).also{
    it.putExtra("username","thatsmanmeet")
    startActivity(it)
}

Now to get data which in our case is a username, which is String, we use getStringExtra() in our second activity.

// Profile Activity
val profileName=intent.getStringExtra("username")

Hence, this was all about Intents and Intent Filters in android which is a pretty important topic in world of android development.