Chapter 24 More About Intents and Tasks
Run NerdLauncher and launch some apps.
Tasks and the Back Stack
Android uses tasks to keep track of the user’s state within each running application. Each application
opened from Android’s default launcher app gets its own task. This is the desired behavior but,
unfortunately for your NerdLauncher, it is not the default behavior. Before you foray into forcing
applications to launch into their own tasks, let’s discuss what tasks are and how they work.
A task is a stack of activities that the user is concerned with. The activity at the bottom of the stack
is called the base activity, and whatever activity is on top is the activity that the user sees. When you
press the Back button, you are popping the top activity off of this stack. If you are looking at the base
activity and hit the Back button, it will send you to the Home screen.
By default, new activities are started in the current task. In CriminalIntent, whenever you started a new
activity, that activity was added to the current task (as shown in Figure 24.5). This was true even if
the activity was not part of the CriminalIntent application, like when you started an activity to select a
crime suspect.
Figure 24.5 CriminalIntent task
The benefit of adding an activity to the current task is that the user can navigate back through the task
instead of the application hierarchy (as shown in Figure 24.6).
Figure 24.6 Pressing the Back button in CriminalIntent
Switching between tasks
Using the overview screen, you can switch between tasks without affecting each task’s state, as
you first saw way back in Chapter 3. For instance, if you start entering a new contact and switch to
checking your Twitter feed, you will have two tasks started. If you switch back to editing contacts,
your place in both tasks will be saved.