Skip to content

Running a service in a separate process

Devrath edited this page Feb 24, 2024 · 4 revisions

Why a separate process

  • Say you have minimized the app and the service is running, It is possible that sometime in the future, the Android OS will remove the activity because of the need for memory.
  • When the activity is removed, The process associated with the activity also is removed.
  • Thus the service and threads attached to the process are also removed.
  • So we can make the service run in a different process and by doing so, Even if the activity is destroyed and the service that is involved in the different process is not destroyed.

How Android picks which process to kill

  • Android has a method that determines which process to kill first and which to kill last.
  • 1st no is the last to be killed and 5th is the first to be killed by the OS

1 Foreground Processes

  • These are the last processes to be killed.
  • This is a type of process that the user is currently interacting with.
  • When we are interacting with the app by clicking buttons, We consider this app process to be the foreground process.

2 Visible Processes

  • These are the processes that do not have the foreground components but still affect the user.
  • Activity in the pause state would be running in a visible process.

3 Service Processes

  • This is for process running started processes.

4 Background Processes

  • If our activity is not visible, Then our process is a background process.

5Empty Processes

  • An empty process is what is left after all the components of the app are destroyed.
  • It's just used for caching.
Clone this wiki locally