Mok
Mok

Reputation: 11

Explicit intent and action performed

I am wondering how does the following code works (it starts an activity). I don't get how does the system figure out what is the action that should be preformed. No action is specified for the Intent. I would have expected a set_action.

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

I am wondering how is it possible to have an Intent which action is not explicitely specified considering what I read in the documentation:

The primary pieces of information in an intent are:

  • action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

  • data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.

I hope it make sense. Thank you for your help.

Upvotes: 0

Views: 943

Answers (2)

Levi Moreira
Levi Moreira

Reputation: 11995

There are two types of intents in Android, implicit intents and explicit intents.

1) Implicit intent

You set an Action, Category and data type and let Android find an activity that fits the specified characteristics (has an intent filter with the specified Action, Category and Data Type).

2) Explicit intent

As the docs says:

An explicit intent is one that you use to launch a specific app component, such as a particular activity or service in your app. To create an explicit intent, define the component name for the Intent object—all other intent properties are optional.

You tell which activity/service to open explicitly. So the system doesn't need to figure out which one to open, you're already telling it to open a specific Activity/Service.

The one you read in the docs is an implicit intent, this is the explicit one:

Intent i = new Intent(this, ActivityTwo.class);

Upvotes: 1

ping li
ping li

Reputation: 1456

There are multiple constructor functions for Intent class. If checking the source code for public Intent(Context packageContext, Class cls), the following info is mentioned:

Create an intent for a specific component. All other fields (action, data, * type, class) are null, though they can be modified later with explicit * calls. This provides a convenient way to create an intent that is * intended to execute a hard-coded class name, rather than relying on the * system to find an appropriate class for you;

Upvotes: 0

Related Questions