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); 
startActivity(intent);


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"); 
startActivity(intent);

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

Action

Description

ACTION_MAIN

Start the activity as the main entry point.

ACTION_VIEW

View the data attached to the intent.

ACTION_ATTACH_DATA

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

ACTION_EDIT

Edit the data attached to the intent.

ACTION_PICK

Pick an item from the data.

ACTION_CHOOSER

Displays all applications that can handle the intent.

ACTION_GET_CONTENT

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

ACTION_DIAL

Dial the number attached to the intent.

ACTION_CALL

Call the person specified in the intent.

ACTION_SEND

Send the data attached to the intent.

ACTION_SENDTO

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

ACTION_ANSWER

Answer the incoming call.

ACTION_INSERT

Insert an empty item into the specified container.

ACTION_DELETE

Delete the specified data from its container.

ACTION_RUN

Run the attached data.

ACTION_SYNC

Perform a data synchronization.

ACTION_PICK_ACTIVITY

Select an activity from a set of activities.

ACTION_SEARCH

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

ACTION_WEB_SEARCH

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

ACTION_FACTORY_TEST

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) {
    startActivity(intent);
}

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
intent.putExtra(Intent.EXTRA_EMAIL,
        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) {
    startActivity(intent);
} else {
    Toast.makeText(this, "No email client found.", 
            Toast.LENGTH_LONG).show();
}


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);
startActivity(intent);

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

Comments

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 *