Intents
Tags: android, androiddevelopment, beginners, intents
Published: 2020-06-02 00:47:39

---
title: Intents
published: true
date: 2020-05-30 00:00:00 UTC
tags: android, androiddevelopment, beginners, intents
series: Android Basics 
canonical_url: https://avinsharma.com/android-basics-intents/
---

### What are intents?

The android documentation describes intents very well:

> An Intent object carries information that the Android system uses to determine which component to start (such as the exact component name or component category that should receive the intent), plus information that the recipient component uses in order to properly perform the action (such as the action to take and the data to act upon).

### Types of Intents

1. Explicit
2. Implicit

Let’s just dive into code samples, I think looking at some code will make a lot of sense.

#### Explicit Intent

In the [app](https://github.com/avin-sharma/Github-Repository-Search) we have been building if I want to start a new activity(say `DetailsActivity.class`) when I click a list item this is how we would do it

```java
Intent intent = new Intent(this, DetailsActivity.class);
intent.putExtra(...); // Send some details to the new activity (OPTIONAL)
startActivity(intent);
```

It is called explicit Intent because we are explicitly telling the intent which component to start. We could start activities in our all like we did in the example above or in some other app, but that app should exist in the user’s device. Basically when we know exactly what we want to start we use explicit intent.

#### Implicit Intent

This is the other part on intents. We use implicit intents when we don’t know or don’t care which app or component is used. Some example would be when we want to open a location on a map or when we want to click a picture we don’t care which app ends up handling our request(from a developer’s perspective that is, if there are multiple apps that can handle our request the user is presented with a selection of choices).

An example of implicit intent to set an alarm.

```java
public void createAlarm(String message, int hour, int minutes) {
    Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
            .putExtra(AlarmClock.EXTRA_MESSAGE, message)
            .putExtra(AlarmClock.EXTRA_HOUR, hour)
            .putExtra(AlarmClock.EXTRA_MINUTES, minutes);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
```

Here is a really helpful link which has most of the [common intents](https://developer.android.com/guide/components/intents-common).

I believe this is enough to get started, there are other things that the intents are used to start like broadcasts and services. We might want to research more when we are doing something more specific but preemptively learning about all the kinds of intents is not particularly helpful, we can always refer back to the documentation when needed.

This was a short one but was an important pit stop since we are going to use intents soon to start details activity on list item click and to navigate to preferences.

I think I want to create a menu next through which we can go to the preferences and then create preferences so that we can change query parameters. Then explore modelview and livedata to handle configuration changes like screen rotations.

As always if you find any errors, have feedback, or just want to say hi please don’t hesitate to leave a comment below.