Identifying Your Intent

Even applications that have only two screens (such as the Task Reminder application) have a great deal happening behind the scenes. One notable interaction that happens between the application and the user is the introduction of new screens as the user tries various features of the application. As with any application that has a rich feature set, the user can interact with each screen independently. The big question that arises is, “How does a user open another screen?”

Screen interaction is handled via the Android intent system. In the following sections, you set up intents that allow the user to navigate from one screen to the next. Thankfully, it’s a simple process. (Turn to Chapter 7 to find out more about the intent system.)

Starting new activities with intents

Activities are initiated via the Android intent framework. An Intent is a class that represents a message that’s placed in the Android intent system (similar to a message-bus type of architecture), and whoever can respond to the intent lets the Android platform know, resulting in either an activity starting or a list of applications to choose from. (This chooser concept is explained in the later section “Creating a chooser.”) You can think of an intent as an abstract description of an operation.

Starting a particular activity is easy. In your ReminderListFragment, type the following code into the onListItemClick() method:

@Override

public void onListItemClick(ListView l, View v, int position, long id) {

super.onListItemClick(l, v, position, id);

Intent i = new Intent(getActivity(), ReminderEditActivity.class);4

i.putExtra(ReminderProvider.COLUMN_ROWID, id);5

startActivity(i);6

}

Here’s a brief explanation of each line:

4 This line creates a new intent using the Intent constructor that accepts the current context, which is the current running activity, as well as a class that the intent system should attempt to start — the ReminderEdit activity.

5 This line places extra data into the Intent object. This intent includes a key/value pair. The key is RowId, and the value is the ID of the view that was clicked. This value is placed into the intent so that the receiving activity (the ReminderEditActivity) can pull this data from the Intent object and use it to load the information about the intent. In Chapter 12, you can see data flowing into the ReminderEditFragment.

6 This line starts the activity from within the current activity. This call places the intent message into the Android intent system and allows Android to decide how to open that screen for the user.

Creating a chooser

At some point, you may run into a particular instance where you need to provide the user with a list of applications that can handle a particular intent. A common example is sharing data with a friend via a common networking tool, such as e-mail, SMS, Twitter, Facebook, or Google Latitude.

The Android intent system was built to handle these types of situations. Though it isn’t used in the Task Reminder application, it can come in handy. The code to display various available options to the user is shown in Listing 9-7.

Listing 9-7: Creating an Intent Chooser

Intent i = new Intent(Intent.ACTION_SEND); →1

i.setType(“text/plain”); →2

i.putExtra(Intent.EXTRA_TEXT, “Hey Everybody!”); →3

i.putExtra(Intent.EXTRA_SUBJECT, “My Subject”); →4

Intent chooser = Intent.createChooser(i, “Who Should Handle this?”); →5

startActivity(chooser); →6

Here’s a brief explanation of each line in Listing 9-7:

1 The creation of a new intent that informs the intent system that the user wants to send, or mail, something.

2 The content type of the message. It can be set to any explicit MIME type. MIME types are case-sensitive, unlike RFC MIME types, so always type them in lowercase letters to specify the type of the intent. Only applications that can respond to this type of intent will then show up in the chooser.

3 Placing extra data into the intent. It’s the body of the message that the application will use. If an e-mail client is chosen, this line comprises the e-mail body. If Twitter is chosen, the message of the tweet is the body. Every application that responds to the intent can handle the extra data in its own, special manner. Don’t expect the data to be handled as you might believe it should be, in the destination application. The developer of this type of application determines how the application should handle the extra data.

4 Similar to line 3, but with a subject extra provided. If an e-mail client responds, this line normally comprises the subject of the e-mail.

5 Creates the chooser. (The Intent object has a static helper method that helps you.) The chooser is itself an intent. You simply provide the target intent (the action that you want to happen) as well as a title for the pop-up chooser that is shown.

6 Starts the intent. It creates the chooser from which you choose an application.

The chooser that’s created from Listing 9-7 is shown in Figure 9-4.

Figure 9-4: The new chooser that was created.

9781118417454-fg0904.tif

If the intent system can find no valid applications to handle the intent, the chooser is created with a message informing the user that no applications can perform the action, as shown in Figure 9-5.

Figure 9-5: The chooser informs the user that Android cannot find a matching application to handle the intent.

9781118417454-fg0905.tif

warning_bomb.eps The chooser is a helpful way to increase the interoperability of an application. However, if you simply call startActivity() with your intent without creating a chooser, your application might crash because Android is giving you full reign and assumes that you know what you’re doing. By not including a chooser, you’re assuming that the destination device has at least one application to handle the intent. If it doesn’t, Android throws an exception (visible via DDMS) to inform you that no class can handle the intent. To the user, your app has crashed.

tip.eps To provide a satisfactory user experience, always provide an intent chooser when firing off intents that are intended for interoperability with other applications. The chooser provides a smooth and consistent usability model that the rest of Android already provides.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset