# Android processes

![](https://3928478158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhjMjdRXwO33Lfo7uCpl6%2Fuploads%2Fgit-blob-8c5d6ef24ae54759175052b237351dccf0432770%2Fmapt.png?alt=media)

## Android startup sequence

### **1. Bootloader**

* Initializes hardware.
* Loads the **Linux kernel** into memory.
* Passes control to the kernel.

### 2. Linux Kernel

* Starts low-level drivers.
* Mounts the root filesystem.
* Starts the **`init`** process (PID 1).

### 3. `init` process (Android Init)

* Reads `init.rc` scripts.
* Starts key system services, including:
  * **`servicemanager`**
  * **`vold`**
  * **`surfaceflinger`**
  * **`zygote`**

### 4. Zygote

* Preloads system classes/resources
* Preloads Android framework classes
* Preloads common libraries
* Forks to create:
  * **System Server** (which manages system services)
  * **App processes** when apps start

***

## Application startup

![](https://3928478158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhjMjdRXwO33Lfo7uCpl6%2Fuploads%2Fgit-blob-de26977dc99df9ca7f6963dada4a91a37588fdbd%2Fapplication_startup.png?alt=media)

### 1. User Action and intent creation

* The user clicks an app icon.
* The **Launcher** is just another app.
* It builds an **Intent** describing which Activity should start.
* It sends this intent to the **Activity Manager Service (AMS)** using **Binder IPC**.

### 2. Activity Manager Service (AMS)

AMS is the system component that manages:

* Intents
* Activity life cycles
* Permissions
* Tasks and back stack

When AMS receives the `startActivity(intent)` request, it:

* Resolves the Intent
* Checks permissions
* Determines whether the app already has a running process

If the app **does not have a process running**, AMS goes to the next step.

### 3. Process creation via Zygote

AMS calls Zygote to ask it to clone itself to create a new process for this application via `Process.start()` (the new process becomes the **application process**).

### 4. Activity Thread + Dalvik VM (ART)

Inside the newly forked process:

* Android runtime starts (Dalvik VM in older Android; now ART)
* It launches the **ActivityThread** class

**ActivityThread** is NOT a thread. It's a manager for the app’s *main thread* responsible for:

* Starting activities
* Running the main (UI) thread
* Managing the Looper (*Looper.loop()* starts the event loop)

### 5. AMS communicates with the new process\*\*

After the process is ready, AMS sends in order:

1. ***BIND\_APPLICATION:*** it tells the new process:
   * Load the app package
   * Initialize classes
   * Prepare resources
2. ***LAUNCH\_ACTIVITY:*** it tells `ActivityThread`:
   * Call the lifecycle methods of the Activity.

### 6. Activity Life Cycle Begins

`ActivityThread` now calls:

1. **onCreate()**
2. **onStart()**
3. **onResume()**

***

## Main components

* `Activity` **class:** unlike programming paradigms in which apps are launched with a `main()` method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.

### Activity lifecycle

![](https://3928478158-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhjMjdRXwO33Lfo7uCpl6%2Fuploads%2Fgit-blob-efb7a66852bb7bc50cc55729402cd1328b9aa936%2Factivity_lifecycle.png?alt=media)
