Processes and Application Lifecycle
Generally, 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 finishes, until the user finishes it or until the system kills it to recover memory for other more important applications.
To determine which processes should be killed when low on memory, Android places each process into an "importance hierarchy" based on the application components (in particular Activity, Service, and BroadcastReceiver) running in them and the state of those components. These process types are (in order of importance):
1) Foreground process : a process is considered to be in the foreground if any of the following conditions hold:
4) Cached process: processes that do not fall under the first types.They often hold one or more Activity instances that are not currently visible to the user (the onStop() method has been called and returned). Provided they implement their Activity life-cycle correctly, when the system kills such processes it will not impact the user's experience when returning to that app: it can restore the previously saved state when the associated activity is recreated in a new process.
1) Foreground process : a process is considered to be in the foreground if any of the following conditions hold:
- It is running an Activity at the top of the screen that the user is interacting with (its onResume() method has been called).
- It has a BroadcastReceiver that is currently running (its BroadcastReceiver.onReceive() method is executing).
- It has a Service that is currently executing code in one of its callbacks (Service.onCreate(), Service.onStart(), or Service.onDestroy()).
2) Visible process: a process is considered visible in the following conditions:
- It is running an Activity that is visible to the user on-screen but not in the foreground (its onPause() method has been called). This may occur, for example, if the foreground Activity is displayed as a dialog that allows the previous Activity to be seen behind it.
- It has a Service that is running as a foreground service, through Service.startForeground() (which is asking the system to treat the service as something the user is aware of, or essentially visible to them).
- It is hosting a service that the system is using for a particular feature that the user is aware, such as a live wallpaper, input method service, etc.
3) Service process: is one holding a Service that has been started with the startService() method. Though these processes are not directly visible to the user, they are generally doing things that the user cares about (such as background network data upload or download), so the system will always keep such processes running unless there is not enough memory to retain all foreground and visible processes.
Services that have been running for a long time (such as 30 minutes or more) may be demoted in importance to allow their process to drop to the cached list described next. This helps avoid situations where long running services that use excessive resources (for example, by leaking memory) prevent the system from delivering a good user experience.
Services that have been running for a long time (such as 30 minutes or more) may be demoted in importance to allow their process to drop to the cached list described next. This helps avoid situations where long running services that use excessive resources (for example, by leaking memory) prevent the system from delivering a good user experience.