Intents in Android: Introduction

Intent is an object that an android component can use to communicate with the Android OS.  There are various components in android, these are:

  • Activity
  • Services
  • Broadcast receivers ,and
  • Content providers.

Intents are multi-purpose communication tools, and the Intent class provides different constructors depending on what we are using the intent to do.

For example, usually, we use intents to tell the ActivityManager which child activity to start from a parent activity, so for this purpose we use following constructor:

public Intent(Context packageContext, Class<?>  cls)

Whenever we want an activity to start a second activity, we use intent. We can think of intent as “intent to do something”. It’s a type of message that allows us to bind separate objects (such as activities) together at runtime. If one activity wants to start a second activity, it does it by sending intent to Android. Android will start the second activity and pass it the intent.

We can create and send an intent using just a couple of lines of code. We start by creating the intent like this:

Intent intent = new Intent(ParentActivity.this, ChildActivity.class);

  • The first parameter tells Android which object the intent is from
  • We can use the word this to refer to the current activity.
  • The second parameter is the class name of the activity that needs to receive the intent.

Once we’ve created the intent, we pass it to Android like this:

startActivity(intent);

This tells Android to start the activity specified by the intent (We discuss this entire process in my upcoming post)

Types of Intents

We typically use intents in two ways:

  • Explicit Intents
  • Implicit Intents

Explicit Intents:

When we create Intent with a Context and a Class object, we are creating an explicit intent. We use explicit intents to start activities from within our application.

Implicit Intents

It may seem strange that two activities within our application must communicate via the ActivityManager, which is outside of our application. However, this pattern makes it easy for an activity in one application to work with an activity in another application. When an activity in our application wants to start an activity in another application, we create an implicit intent.

Use of Intents

  • In perhaps its most common use, an Intent object allows us to switch between activities.
  • But, of course, activities are classes. So, what happens to their data when we switch between them? Intents handle this problem for us, as well, by allowing us to pass data between activities.
  • Intents aren’t just about wiring up the activities of our app. They also make it possible to interact with other apps. For example, we could provide a link in our app for the user to send e-mail, make a phone call, interact with social media, and open a web page in a browser and have the e-mail, dialer, browser, or relevant social media app do all the work.

This is my first post on Intents (an introductory post), which will get you stated with Intent basics. But I hope you get the brief idea about what is Intents and why we use them. In my upcoming post we discuss how to start a child activity from a parent activity (Switching between activities).

Thanks a lot!!