Android Tutorial: Activity Related Intents

In the article Activity Related Intents, you learned that you can start a new activity by passing an intent to the startActivity method. You can also call startActivityForResult if you want a result from the invoked activity. Related article: Android Tutorial: The Activity Lifecycle

Here is the code that activates an activity in the project:

Intent intent = new Intent(this, SecondActivity.class); 

And often you want to pass additional information to the invoked activity, which you can do by attaching the information to the intent. In the previous example, you did so by calling the putExtra method on the Intent:

Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("message", "Message from First Screen"); 

An intent that is constructed by passing to it an activity class is called an explicit intent. The Intent in SecondActivityDemo is such an example.

You can also create an implicit intent, in which case you do not specify an activity class. Rather, you pass to the Intent class’s constructor an action, such as ACTION_SEND, and let the system decide which activity to start. If there is more than one activity that can handle the intent, the system will normally ask the user to choose.

ACTION_SEND is a constant in the Intent class. Table 1.1 shows a list of actions that can start an activity as defined in the Intent class.

Activity Related Intents




Start the activity as the main entry point.


View the data attached to the intent.


Attach the data that has been added to the intent to some other place.


Edit the data attached to the intent.


Pick an item from the data.


Displays all applications that can handle the intent.


Allow the user to select a particular kind of data and return it.


Dial the number attached to the intent.


Call the person specified in the intent.


Send the data attached to the intent.


Send a message to the person specified in the intent data.


Answer the incoming call.


Insert an empty item into the specified container.


Delete the specified data from its container.


Run the attached data.


Perform a data synchronization.


Select an activity from a set of activities.


Perform a search using the specified string as the search key.


Perform a web search using the specified string as the search key.


Indicate this is the main entry point for factory tests.

Intent actions for starting an activity

Not all intents can be used to start an activity. To make sure an Intent can revolve to an activity, call its resolveActivity method before passing it to startActivity:

if (intent.resolveActivity(getPackageManager()) != null) {

An intent that cannot resolve to action will throw an exception if passed to startActivity.

For example, here is an Intent to send an email.

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822"); // required
        new String[] {"[email protected]"}); // optional
intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); // optional
intent.putExtra(Intent.EXTRA_TEXT   , "body"); // optional

// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
} else {
    Toast.makeText(this, "No email client found.", 

If multiple applications can handle an Intent, the user will be able to decide whether to always use the selected application in the future or to use it just for this occasion. You can force a chooser to appear each time (regardless of whether or not the user has decided to use the same app), by using this code:

startActivity(Intent.createChooser(intent, dialogTitle));

where dialogTitle is the title of the Chooser dialog.

As another example, the following code sends an ACTION_WEB_SEARCH intent. Upon receiving the message, the system will open the default web browser and tell the browser to google the search key.

String searchKey = "Buffalo";
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager.QUERY, searchKey);

In this article, we learned about the activity intents. And showed how to start an activity from another activity.


Android Tutorial: Creating Another Activity - Digital Juices

[…] The main activity of an Android application is started by the system itself when the user selects the app icon from the Home screen. In an application with multiple activities, it is possible (and easy) to start another activity. In fact, starting an activity from another activity can be done simply by calling the startActivity method like this. Next article: Android Tutorial: Activity Related Intents […]

Leave a Reply

Your email address will not be published. Required fields are marked *