Chapter 24 More About Intents and Tasks
Challenge: Icons
You used ResolveInfo.loadLabel(PackageManager) in this chapter to present useful names in your
launcher. ResolveInfo provides a similar method called loadIcon() that retrieves an icon to display
for each application. For a small challenge, add an icon for each application to NerdLauncher.
For the More Curious: Processes vs Tasks
All objects need memory and a virtual machine to live in. A process is a place created by the OS for
your application’s objects to live in and for your application to run.
Processes may own resources managed by the OS, like memory, network sockets, and open files.
Processes also have at least one, possibly many, threads of execution. On Android, your process will
also always have exactly one virtual machine running.
While there are some obscure exceptions, in general every application component in Android is
associated with exactly one process. Your application is created with its own process, and this is the
default process for all components in your application.
(You can assign individual components to different processes, but we recommend sticking to the
default process. If you think you need something running in a different process, you can usually
achieve the same ends with multithreading, which is more straightforward to program in Android than
using multiple processes.)
Every activity instance lives in exactly one process and is referenced by exactly one task. But that is
where the similarities between processes and tasks end. Tasks contain only activities and often consist
of activities living in different application processes. Processes, on the other hand, contain all running
code and objects for a single application.
It can be easy to confuse processes and tasks because there is some overlap between the two
ideas and both are often referred to by an application name. For instance, when you launched
CriminalIntent from NerdLauncher, the OS created a CriminalIntent process and a new task for which
CrimeListActivity was the base activity. In the overview screen, this task was labeled CriminalIntent.
The task that an activity is referenced by can be different from the process it lives in. For example,
consider the CriminalIntent and contact applications and walk through the following scenario.
Open CriminalIntent, select a crime from the list (or add a new crime), and then press CHOOSE
SUSPECT. This launches the contacts application to choose a contact. The contact list activity is
added to the CriminalIntent task. This means that when your user presses the Back button to navigate
between different activities, he or she may be unknowingly switching between processes, which is
nifty.