The order in which processes are killed to reclaim resources is determined by the priority of the hosted applications. An application's priority is equal to its highest-priority component.
If two applications have the same priority, the process that has been at a lower priority longest will be killed first. Process priority is also affected by interprocess dependencies; if an application has a dependency on a Service or Content Provider supplied by a second application, the secondary application will have at least as high a priority as the application it supports.
All Android applications will remain running and in memory until the system needs resources for other applications.
Figure 3-3 shows the priority tree used to determine the order of application termination.
It's important to structure your application correctly to ensure that its priority is appropriate for the work it's doing. If you don't, your application could be killed while it's in the middle of something important.
The following list details each of the application states shown in Figure 3-3, explaining how the state is determined by the application components comprising it:
> Active processes Active (foreground) processes have application components interacting with the user. These are the processes Android is trying to keep responsive by reclaiming resources. There are generally very few of these processes, and they will be killed only as a last resort.
Active processes include:
> Activities in an "active" state; that is, those in the foreground responding to user events. You will explore Activity states in greater detail later in this chapter. FIGURE 3-3
> Broadcast Receivers executing onReceive event handlers.
> Services executing onStart, onCreate, or onDestroy event handlers.
> Running Services that have been flagged to run in the foreground.
> Visible processes Visible but inactive processes are those hosting "visible" Activities. As the name suggests, visible Activities are visible, but they aren't in the foreground or responding to user events. This happens when an Activity is only partially obscured (by a non-full-screen or transparent Activity). There are generally very few visible processes, and they'll be killed only under extreme circumstances to allow active processes to continue.
> Started Service processes Processes hosting Services that have been started. Services support ongoing processing that should continue without a visible interface. Because background Services don't interact directly with the user, they receive a slightly lower priority than visible Activities. They are still considered foreground processes and won't be killed unless resources are needed for active or visible processes. You'll learn more about Services in Chapter 9.
> Background processes Processes hosting Activities that aren't visible and that don't have any running Services. There will generally be a large number of background processes that Android will kill using a last-seen-first-killed pattern in order to obtain resources for foreground processes.
> Empty processes To improve overall system performance, Android will often retain an application in memory after it has reached the end of its lifetime. Android maintains this cache to improve the start-up time of applications when they're relaunched. These processes are routinely killed as required.
Was this article helpful?
Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.