Last Updated:

Introduction to Android Intent - Start an Activity using an Intent

In our last Tutorial on Android, we created our first Android Hello World app. In this Android tutorial, we will delve into the basics and get acquainted with such a useful Android mechanism as Intent, namely what it is, why it is and how to use it.

What is Intent in Android?

Android Intent (a literal translation like "Intent" is still possible, but I prefer to use the word in the original or read as "intent"). And so, it is a data structure that contains a description of the operation to be performed.
One of the most powerful features of the Intent mechanism is that you can asynchronously send messages to other (activities) or services.
In this tutorial, we'll focus on the types and handling of Android Intents.

Types of Android Intent

  • An explicit intent explicitly identifies a component by name. More often than not, in our applications, we will use explicit intent to run a component. The essence of Explicit intent is that you know the name of the Activity or Service class that you want to run. When you use an explicit intent, the system immediately starts the application component specified in the .Intent
  • Implicit intent does not define a specific component, but simply states some action, allowing components of other applications to process it. When you create an implicit Android intent, the system finds all possible components that can handle the claimed action and prompts you to select one from the list found.

How do I use Android Intent to launch an Activity?

To create an object and run with it, you need to pass in the parameters an object-Activity of the class that calls and the name of the Activity that you want to run:IntentActivity

The Activity under the name will be launched in the following order:ToActivity.class

  1. To create a new instance of the class ToActivity.class
  2. this instance is placed at the top of the current operations stack
  3. Activity appears on the screen.ToActivity.class

Using Flags in Intentions (Android Intent Flags)

The flags defined in the Intent object act as metadata. They are used by the Android system as a guide to launching and handling these very intentions.
You can change the behavior of objects by specifying flags (by a method) before calling a method, for example:

This means that the activity transferred to the Intent object will not be started if there is already such an Activity in the stack of running taskes.

Working with Intent on the example of starting one activity from another

Let's create a new project to work with Intent. In this case, we will be satisfied with the simple Hello Android project described in this tutorial. We will work in Android Studio. If you are not familiar with it, then use any IDE that suits you.

Once created, your project should look something like this:

This project consists of two Activities: and . We'll run the DetailsActivity from mainActivity.

To start an activity, we need a button that will be clicked to create an intent and launch an Activity. If you are not familiar with the widget (button), follow my other lesson at this link.Button

The MainActivity code will be as follows:

The layout will be as follows:activity_main.xml

The DetailsActivity code will be as follows:

The layout is as follows:activity_details.xml

The configuration file will look like this:AndroidManifest.xml

Create these files and run the application. At startup, it will look like this:

After clicking the "Run Activity" button, you will have a new one:


That's all work with Intent on Android. In this tutorial, we learned what An Intent is, why you need intent, and how to use it in your apps. Stay tuned for android programming for beginners.