The chart above is familiar to all Android developers, but there are some important points hidden in it that we’ve missed. Let’s comb it together.

In the book Exploring the Art of Android Development, the life cycle analysis of activities is divided into two parts: one is the life cycle in typical cases, and the other is the life cycle in abnormal cases.

This article first carries on some summary to the book content key point.

Life cycle analysis in a typical case

Under normal circumstances, an Activity goes through the following life cycle. 1. OnCreate indicates that the Activity is being created. 2. OnRestart indicates that the Activity is being restarted. This is usually caused by user action. OnRestart will appear. 3. OnStart indicates that the Activity is being started and the Acivity is about to start. At this point, the Acivity is set to visible, but it is not in the foreground and cannot interact with the user. 4. OnResume indicates that Activityi is visible and appears at the front desk to start the activity. 5. OnPause Indicates that the Activity is being stopped. The onPause of the previous Activity must be completed before the onResume of the new Activity can be executed. 6. OnStop indicates that the Activity is about to stop. You can do some heavy recycling work here, but also not too time-consuming. 7. OnDestroy indicates that the Activity is about to be destroyed, so you can do some recycling and resource release.

The key point1. When you start a new Activity with a transparent theme, the current Activity does not call onStop. 2. OnStart and onStop are called back and forth from whether the Activity is visible, while onResume and onPause are called back and forth from whether the Activity is in the foreground. 3. Do not perform heavyweight operations in onPause because a new Activity can Resume only after onPause is complete.

Life cycle analysis in abnormal cases

The Activity can be killed in unusual circumstances, such as when resource-related system configurations change and the system runs out of memory.

InstanceState abnormal state causes the Activity to be killed and recreated: OnPause, onStop, and onDestroy are called when the Activity is destroyed, and onSaveInstanceState is called to save the current state of the Activity because the Activity is terminated abnormally. This method is called before onStop. After the Activity is recreated, onRestoreInstanceState is called and the onRestoreInstanceState and onCreate methods are passed as arguments to the Bundle object that was saved when the Activity was destroyed. This method is called after onStart.

The key point

  1. The system only calls onSaveInstanceState and onRestoreInstanceState to save and restore the instance state if the Activity terminates abnormally.
  2. When using savedInstance in onCreate, be careful to null
  3. When an Activity is killed due to insufficient memory, the system kills the process in which the target Activity resides in the order of process priority.

Android process priority

Android divides the priorities of processes into five levels, in descending order:

Foreground Process It indicates that the user is interacting with the process, and The Android system marks a process as a foreground process based on the following criteria: * The process holds an Activity that the user is interacting with (i.e., the Activity lifecycle method leads to the onResume() method). * This process holds a Service that is bound to an Activity that the user is interacting with. * The process holds a foreground mode Service (that is, the Service that calls the startForegroud() method). * The process holds a Service that is performing lifecycle methods (onCreate(), onStart(), onDestroy(), and so on). * The process holds a BroadcastReceiver that is executing onReceive(). In general, there are not many foreground processes. Killing foreground processes is a last resort for the operating system. The foreground process is also killed when it runs out of memory.

Visible processes. It indicates that although the process does not hold any foreground components, it can still influence the interface that the user sees. The Android system marks a process as visible based on the following criteria: * The process holds a non-foreground Activity that is still visible to the user (that is, the Activity calls onPause()). For example, when an activity starts a dialog, the activity is blocked by the dialog. * The process holds a Service bound to a visible (or foreground) Activity.

A Service process. All services are classified as Service processes except those that meet the criteria for foreground and visible processes.

Background processes. A process that holds an invisible Activity that calls the onStop() method is a background process. In general, there are many background processes. When running out of memory, the process that has not been used for the longest time will be recycled first among all the background processes according to the LRU (recently used) rule.

An Empty process. A process that does not hold any active components. The purpose of keeping this process is to cache it so that it can respond more quickly the next time a component in the process is started. When resources are tight, the system will balance the process cache and the underlying kernel cache for recycling.

If a process meets multiple priority levels, the Android system takes the highest priority level as the priority of the process. For example, if a process holds a Service (Service process level) and a foreground Activity (foreground process level), the operating system marks the process as a foreground process.

It is also important to note that if a process provides a service to another process, that process has no lower priority than the process receiving the service. For example, if the Content Provider in process A provides services for process B, or if A Service in process A is bound to A component process in process B, process A must have the same or higher priority than process B.

#### Reference: Android Development Art Exploration Lin Jiannan Android process priority