482 CHAPTER 13: Android Service Class and Threads: Background Processing
processes, because they have no user interface screen, and are running asynchronously in the
background, are not directly tied to anything that a user sees on a display or is interacting with.
They are still important to Android application development, which is why I have an entire chapter
covering them.
Since a Service is still performing a task that the end-user wants to complete, such as playing
an album of music in the background or downloading a file over the network, Android will keep a
process that contains a Service object active, unless there is not enough memory to support them
along with foreground and visible processes.
Background Process
The second lowest priority process level is a background process. This is a process that contains an
Activity subclass that is not currently visible to the end-user. An example of this is when your Activity
subclass’s .onStop( ) method has been called. Since a background process has no detectible impact
on the user experience, Android will terminate them whenever it becomes necessary to recover
system memory for higher priority level processes (foreground, visible, or Service). There are often
several background processes running, and Android keeps background processes in something
called the LRU (Least Recently Used) list. This list guarantees that a process containing an Activity
that was most recently utilized by the user is the last process to be terminated.
It is important to note that if your Activity subclasses implement their lifecycle methods correctly and
save their current states using the now-familiar savedInstanceState Bundle object, then terminating
that Activity subclass process will not have any discernible effect on the user experience. This is
because when a user navigates back to the user interface screen for that Activity, a process will
again be started for it, and your Activity subclass will restore all of its visible states by loading your
savedInstanceState Bundle object contents.
Empty Process
The lowest priority process level is the empty process. This is a process that does not hold any
currently active application components. If you are wondering why these empty process would be
kept in system memory at all, the strategic reason to keep an empty process alive is for memory
caching optimization, which improves the start-up time the next time a component, most probably
an Activity, needs to run inside the process. The Android operating system will terminate these
empty processes once the system memory is full, in an attempt to balance the overall system
memory resources between the various process caches and with its underlying Linux kernel memory
caches, which are so low level that developers cannot even access them.
Increasing Priority Level Rank
It is important to notice that process priority level rank might be increased because another process
is dependent on a process. Any Android process that is currently servicing another process cannot
be ranked lower than the process it is servicing. This is a logical behavior, if you think about it. Let’s
say the Content Provider (a database or a data store, covered in the next chapter) that is contained
in process 01 is busy providing content to a user interface Activity in Process 02, or a Service object
in process 01 is bound to an application component in Process 02. In these scenarios, Process 01