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. And, on Android, your process will always have exactly one virtual machine running.

Before Android 4.4 (KitKat), Dalvik was the process virtual machine used by the Android OS. Whenever a process started, a new instance of a Dalvik virtual machine would spawn to house that process. Android Runtime (ART) has since replaced Dalvik, becoming the accepted process virtual machine from Android 5.0 (Lollipop) on.

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 multi-threading, 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 MainActivity 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 Contacts 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, they may be unknowingly switching between processes, which is nifty.

However, the contact list activity instance is actually created in the memory space of the Contacts app’s process, and it runs on the virtual machine living in the Contacts application’s process. (The states of the activity instances and task references of this scenario are depicted in Figure 23.12.)

Figure 23.12  Task referencing multiple processes

Task referencing multiple processes

To explore the idea of processes versus tasks further, leave CriminalIntent up and running on the contact list screen. (Make sure the Contacts app itself is not listed on the overview screen. If so, clear the Contacts app task.) Press the Home button. Launch the Contacts app from the Home screen. Select a contact from the list of contacts (or select to add a new contact).

When you do this, new contact list activity and contact details instances will be created in the Contacts application’s process. A new task will be created for the Contacts application, and that task will reference the new contact list and contact details activity instances (as shown in Figure 23.13).

Figure 23.13  Process referenced by multiple tasks

Process referenced by multiple tasks

In this chapter, you created tasks and switched between them. What about replacing Android’s default overview screen, as you are able to do with the Home screen? Unfortunately, Android does not provide a way to do this. Also, you should know that apps advertised on the Play Store as task killers are, in fact, process killers. Such apps kill a particular process, which means they may be killing activities referenced by other applications’ tasks.

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

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