page.title=Processes and Application Life Cycle @jd:body
In most cases, every Android application runs in its own Linux process. This process is created for the application when some of its code needs to be run, and will remain running until it is no longer needed and the system needs to reclaim its memory for use by other applications.
An unusual and fundamental feature of Android is that an application process's lifetime is not directly controlled by the application itself. Instead, it is determined by the system through a combination of the parts of the application that the system knows are running, how important these things are to the user, and how much overall memory is available in the system.
It is important that application developers understand how different application components (in particular {@link android.app.Activity}, {@link android.app.Service}, and {@link android.content.BroadcastReceiver}) impact the lifetime of the application's process. Not using these components correctly can result in the system killing the application's process while it is doing important work.
A common example of a process life-cycle bug is a {@link android.content.BroadcastReceiver} that starts a thread when it receives an Intent in its {@link android.content.BroadcastReceiver#onReceive BroadcastReceiver.onReceive()} method, and then returns from the function. Once it returns, the system considers the BroadcastReceiver to be no longer active, and thus, its hosting process no longer needed (unless other application components are active in it). So, the system may kill the process at any time to reclaim memory, and in doing so, it terminates the spawned thread running in the process. The solution to this problem is to start a {@link android.app.Service} from the BroadcastReceiver, so the system knows that there is still active work being done in the process.
To determine which processes should be killed when low on memory, Android places each process into an "importance hierarchy" based on the components running in them and the state of those components. These process types are (in order of importance):
There will only ever be a few such processes in the system, and these will only be killed as a last resort if memory is so low that not even these processes can continue to run. Generally, at this point, the device has reached a memory paging state, so this action is required in order to keep the user interface responsive.
When deciding how to classify a process, the system will base its decision on the most important level found among all the components currently active in the process. See the {@link android.app.Activity}, {@link android.app.Service}, and {@link android.content.BroadcastReceiver} documentation for more detail on how each of these components contribute to the overall life-cycle of a process. The documentation for each of these classes describes in more detail how they impact the overall life-cycle of their application.
A process's priority may also be increased based on other dependencies a process has to it. For example, if process A has bound to a {@link android.app.Service} with the {@link android.content.Context#BIND_AUTO_CREATE Context.BIND_AUTO_CREATE} flag or is using a {@link android.content.ContentProvider} in process B, then process B's classification will always be at least as important as process A's.