More About Process, Threads, Stack in Android

by Andres Amador / from beautifuldecay.com

Here I’m trying to find new way to explain and study this so important subject. Let me try to explain process and threads in a new easy way, without too much theory.

When you click in a app icon for the first time, a process, Linux process, will be started to host the app and its components (Activity, Service, Broadcast Receiver and Content Providers). So, the main activity which is launched when the app is started is hosted in this process as a component. Two others elements are created too when the process is started: the main thread or UI thread is created and a back stack to host the activity. See this image below:

  • A user click to start an app, so:
    • A process is create to host the app and its components
    • UI Thread is created
    • Back Stack is created
    • Main Activity is launched
    • Main Activity is executed on the UI Thread

Desenho sem título(1)

Every Android component is started in this process and will be run in the UI Thread by default, unless get something done.

Lets talk about state of process. States are important because there is a rank of priority ordering processes by its state. A great way to understand the ranking is: Which process hosts a component important for the user and should be kept running at all costs? The answer is: Process who hosts foreground activity and services, because foreground activity is the activity which the user is interacting and services because they are generally doing heavy things that the user cares about even it always happens in background, such as playing music in the background or downloading data on the network.

Let’s detail this rank and take a look that it really prioritize what is important for the user:

____________________________________

Highest Priority – Foreground Process has an:

  • Activity resumed (onResume()) or/and
  • Service in foreground, executing its lifecycle or/and
  • Broadcast Receive executing onReceive()

Second Highest Priority – Visible Process has an:

  • Activity paused (onPause()) or/and
  • Service bound to a visible activity

Third Highest Priority – Service Process has an:

  • Service running and does not belong to 2 cases above.
  • no user interacting

Second Lowest Priority – Background Process has an:

  • A stopped activity (onStop()) or/and
  • no user insteracting
    OBS: This kind of process is kept in a list LRU (least recently used) to ensure that the last application used is the last to be eliminated when there is no empty process to be eliminated.

First Lowest Priority – Empty Process

  • Kept only for cache purpose

An important exception for this rank:

In addition, a process’s ranking might be increased because other processes are dependent on it—a process that is serving another process can never be ranked lower than the process it is serving. – Android Documentation


Ok…This process drawn in this post is in foreground, because there is a back stack in foreground, because the user is interacting with ActivityA. So, this process has higher priority! If the user opens another app and start to interact with it, the other process, who host the other app and its main activity, goes to foreground and my last process goes to background, losing priority.

Could I be clear? =).. try to draw situations as I explained and check states and priorities step by step to understand better.

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

w

Conectando a %s