When reusing an activity owned by others handle the case where no activity matches

Your applications can re-use activities made available from other applications. In doing so, you cannot presume your intent will always be resolved to a matching external activity — you must handle the case where no application installed on the device can handle the intent.

o Start an activity not expecting a result - This approach is open-ended. An example is choosing an house address in an email message (or web page), where the Maps activity is started to map the location. No result from maps is expected to be returned to the email message; the user can return by pressing the BACK key. (Such an activity is started with startActivityQ.)

• Launch your activity only from within another application - The previous cases of sharing by way of Email, Gmail, Messaging and Picassa (from within Gallery) are all activities that can also be started from icons in the application launcher at Home. In contrast, the activities for cropping a picture and attaching a file cannot be started from Home, because they do not stand alone and require a context.

In fact, not all applications have icons and can be started from Home. Take for example a small app that is infrequently used and replaces existing functionality, that already has a natural entry point inside an existing application. For example, an Android phone typically has a built-in ringtone picker that can be selected from the sound settings of the Settings application. A custom ringtone picker application that you write could be launched by an intent identical to the built-in ringtone picker. At the point where the user chooses "Phone ringtone", they are presented with a dialog letting them choose between "Android System" and your ringtone picker (and letting them save their choice) as shown in the following figure. A ringtone is something you set infrequently, and already has a well-defined starting point, so probably does not need an application icon at Home.

• Launch two or more main activities within a single application from separate icon at Home - As we have defined it, all the code in a single .apk file is considered to be one application.You can write an application that contains two main activities launchable from Home.

The Camera.apk application is a good example of an application that contains two independent main activities — Camera and Camcorder — that each have their own icons in application launcher, that can be launched separately, and so appear to the user as separate applications. They both share use of the same lens, and both store their images (still and moving) in the Gallery.

In order for your application to contain two different, independent activities launchable from Home, you must define them to be associated with different tasks. (This means setting the main activity for each task to a different task affinity — in this case, "com.android.camera" and "com.android.videocamera".)

Contacts and Dialer are another example of two main activities launchable from Home that reside in the same application.

• Making your application available as a widget - An application can also display a portion of itself as an app widget, embedded in Home or another application, and receive periodic updates.

0 0

Post a comment