Activity lifecycle analysis

Break the Activity lifecycle into two parts:

1. Typical life cycle

Normally, an Activity goes through the following lifecycle:

  1. OnCreate: Indicates that the Activity is being created
  2. OnRestart: Indicates that the Activity is being restarted
  3. OnStart: Indicates that the Activity is being started and is visible but not in the foreground
  4. OnResume: Indicates that the Activity is visible and in the foreground
  5. OnPause: Indicates that the Activity is being stopped (you can do a save state to stop animation and other non-time-consuming actions)
  6. OnStop: Indicates that the Activity is about to stop (heavyweight recycling can be done)
  7. OnDestroy: Indicates that the Activity is about to be destroyed

  • First start: onCreate->onStart->onResume;
  • Start a new Activity or return to the desktop: onPause->onStop. If the new Activity is opened with a transparent theme, onStop is not called;
  • When returning to the original Activity: onRestart->onStart->onResume;
  • When the return key is pressed: onPause->onStop->onDestroy

OnPause (A)->onCreate(B)->onStart(B)->onResume(B)->onStop(A)

2. Life cycle in abnormal cases

There are two main types of anomalies:

A change in the system configuration file associated with the resource causes the Activity to be killed and rebuilt

For example, vertical screen switch: When the system configuration is changed

  • The Activity is destroyed (onPause->onStop->onDestroy);
  • . Since it was terminated in an exception, onSaveInstanceState is called to save the current Activity state. OnSaveInstanceState is called before onStop and has no sequential relationship with onPasue;
  • When the Activity is recreated, onRestoreInstanceState is called (before onStart), The Bundle saved in onSaveInstanceState is passed as an argument to onCreate and onRestoreInstanceState

Normally, an Activity does not call onSaveInstanceState or onRestoreInstanceState. You can use onRestoreInstanceState to determine whether the Activity is rebuilt. The Activity automatically does some recovery work for us, such as the current view structure and data (textbox user input data, ListView scroll position)

When an Activity is destroyed abnormally, we can save some data in onSaveInstanceState and wait for the Activity to rebuild in onCreate or onRestoreInstanceState to retrieve the state information. The difference between onCreate and onRestoreInstanceState is that the Bundle may be empty when onCreate is started normally, whereas onRestoreInstanceState must not be empty when onRestoreInstanceState is called.

Low priority activities are killed due to insufficient memory resources

Activities fall into three categories based on priority:

  1. Foreground Activity: the Activity that is interacting with the user. It has the highest priority.
  2. Visible but not foreground (visible but not interactive) : For example, an Activity has a pop-up window and cannot interact;
  3. Background Activity: An Activity that has already executed onStop

When the system runs out of memory, the system kills the target Activity according to the above priority and stores and restores data through the subsequent onSaveInstanceState and onRestoreInstanceState.

There are four launch modes for an Activity

  1. Standard: the standard mode is also the default mode. A brand new instance is created for each startup.
  2. SingleTop: Top of stack reuse mode. In this mode, no new instance is created if the Activity is at the top of the stack. OnNewIntent will be called to receive the new request, no lower than onCreate and onStart.
  3. SingleTask: In-stack reuse mode. The upgraded version of singleTop, if there is an instance in the stack, is reused and all activities on that instance are cleared.
  4. SingleInstance: the system creates a separate task stack for it, and this instance runs independently in a task that has only this instance. No other activities are allowed in this task.

Method to specify the startup mode

Specify startup mode in AndroidMenifest

<activity android:name=".singletop.SingleTopActivity" android:launchMode="singleTop">

Specify the startup mode by setting flag in the Intent

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TASK);

Common usage scenarios for the four boot modes

LauchMode scenario
standard Mostly ordinary scenes
singleTop Login registration page, WXPayEntryActivity, WXEntryActivity
singleTask Home page, order page, payment, scan code, etc
singleInstance System Launcher, lock screen, call