Activity and Task Design Guidelines

来源:百度文库 编辑:神马文学网 时间:2024/05/05 16:20:53
Activity and task design quickview
Activities are the main building blocks of Android applications. In addition to writing your own activities, you are free to re-use activities from many other applications through intents. You can enable activities in your application to be started from intents in other applications. In nearly all cases, the activity stack just works as expected. In a couple of cases you might need to ensure the right thing happens by setting a string or flag.
In this document
Applications, Activities, Activity Stack and TasksA Tour of Activities and Tasks Starting an Activity from Home Navigating Away from an Activity Re-using an Activity Replacing an Activity Multitasking Launching from Two Entry Points Intents Switching Between Tasks Design Tips Don't specify intent filters in an activity that won't be re-used Handle case where no activity matches Consider how to launch your activities Allow activities to add to current task Notifications should let user easily get back Use the notification system Don't take over BACK key unless you absolutely need to See also
Application Fundamentals
This document describes core principles of the Android application framework, from a high-level, user-centric perspective useful to interaction and application designers as well as application developers.
It illustrates activities and tasks with examples, and describes some of their underlying principles and mechanisms, such as navigation, multitasking, activity re-use, intents, and the activity stack. The document also highlights design decisions that are available to you and what control they give you over the UI of your application.
This document draws examples from several Android applications, including default applications (such as Dialer) and Google applications (such as Maps). You can try out the examples yourself in the Android emulator or on an Android-powered device. If you are using a device, note that your device may not offer all of the example applications used in this document.
Be sure to look at theDesign Tips section for guidelines, tips, and things to avoid. This document is a complement toApplication Fundamentals, which covers the underlying mechanics for programmers.
Applications, Activities, Activity Stack and Tasks
Four fundamental concepts in the Android system that are helpful for you to understand are:
Applications Activities Activity Stack Tasks
Applications
An Android application typically consists of one or more related, loosely bound activities for the user to interact with, typically bundled up in a single file (with an .apk suffix). Android ships with a rich set of applications that may include email, calendar, browser, maps, text messaging, contacts, camera, dialer, music player, settings and others.
Android has an application launcher available at the Home screen, typically in a sliding drawer which displays applications as icons, which the user can pick to start an application.
Activities
Activities are the main building blocks of Android applications. When you create an application, you can assemble it from activities that you create and from activities you re-use from other applications. These activities are bound at runtime, so that newly installed applications can take advantage of already installed activities. Once assembled, activities work together to form a cohesive user interface. An activity has a distinct visual user interface designed around a single, well-bounded purpose, such as viewing, editing, dialing the phone, taking a photo, searching, sending data, starting a voice command, or performing some other type of user action. Any application that presents anything on the display must have at least one activity responsible for that display.
When using an Android device, as the user moves through the user interface they start activities one after the other, totally oblivious to the underlying behavior — to them the experience should be seamless, activity after activity,task after task.
An activity handles a particular type of content (data) and accepts a set of related user actions. In general, each activity has alifecycle that is independent of the other activities in its application or task — each activity is launched (started) independently, and the user or system can start, run, pause, resume, stop and restart it as needed. Because of this independence, activities can be re-used and replaced by other activities in a variety of ways.
The Dialer application is an example of an application that consists basically of four activities: dialer, contacts list, view contact, and new contact, as shown in the following screenshots:
Dialer
Contacts
View Contact
New Contact
Here are other examples of applications and the activities they might contain:
Email - activities to view folders, view list of messages, view a message, compose a message, and set up an account Calendar - activities to view day, view week, view month, view agenda, edit an event, edit preferences, and view an alert Camera - activities for running the camera, viewing the list of pictures, viewing a picture, cropping a picture, running the camcorder, viewing the list of movies, and viewing a movie Game - one activity to play the game, typically another for setup Maps - one activity to view a location on a map, a second for lists (such as turn list or friend list), and a third for details (friend location, status, photo)
An activity is the most prominent of four components of an application. The other components are service, content provider and broadcast receiver. For more details on activities, see Activity inApplication Components.
Activity Stack
As the user moves from activity to activity, across applications, the Android system keeps a linear navigation history of activities the user has visited. This is the activity stack, also known as the back stack. In general, when a user starts a new activity, it is added to the activity stack, so that pressing BACK displays the previous activity on the stack. However, the user cannot use the BACK key to go back further than the last visit to Home. The adding of an activity to the current stack happens whether or not that activity begins a newtask (as long as that task was started without going Home), so going back can let the user go back to activities in previous tasks. The user can get to tasks earlier than the most recent Home by selecting its root activity from the application launcher, a shortcut, or the "Recent tasks" screen.
Activities are the only things that can be added to the activity stack — views, windows, menus, and dialogs cannot. That is, when designing the navigation, if you have screen A and you want the user to be able go to a subsequent screen B and then use the BACK key to go back to screen A, then the screen A needs to be implemented as an activity. The one exception to this rule is if your application