As the user opens, switches, and returns to the corresponding application, the UIAbility instance in the application transitions between different states in its lifecycle.The UIAbility class provides a series of callbacks through which you can know that a state of the current UIAbility instance has changed, that it will go through the creation and destruction of the UIAbility instance, or that the A front-to-back state switch has occurred in the UIAbility instance.
The life cycle of UIAbility includes four states, Create, Foreground, Background, and Destroy, as shown below
Create Status
Create state is triggered during the application loading process when the creation of the UIAbility instance is complete, and the system will call the onCreate() callback. You can initialize the page in this callback, such as variable definition resource loading, etc., for the subsequent UI display
WindowStageCreate and WindowStageDestroy states
After the UIAbility instance is created, before entering the Foreground, the system will create a WindowStage. after the WindowStage is created, it will enter the onWindowStageCreate() callback, where you can set up the UI to load, and set up WindowStage's event subscription.
In the onWindowStageCreate() callback, set the page to be loaded by the loadContent() method and subscribe to the WindowStage events (focus gained/focus lost, cut to foreground/cuts to background, foreground interactive/foreground non-interactive) by calling the on('windowStageEvent') method as needed. The Corresponds to the onWindowStageCreate() callback. Before the UIAbility instance is destroyed, it will go to onWindowStageDestroy() callback, in which UI resources can be released. The corresponding onWindowStageWillDestroy() callback is executed before the WindowStage is destroyed, at which time the WindowStage can be used.
Foreground and Background states
The Foreground and Background states are triggered when the UIAbility instance switches to the foreground and switches to the background respectively, corresponding to the onForeground() callback and onBackground() callback.
The onForeground() callback, triggered before the UIAbility's UI is visible, such as when the UIAbility switches to the foreground. You can request resources needed by the system in the onForeground() callback, or re-request resources released in onBackground().
The onBackground() callback is triggered after UIAbility's UI is completely invisible, such as when UIAbility switches to the background. You can release useless resources when the UI is not visible in the onBackground() callback, or perform more time-consuming operations in this callback, such as state saving.
When the application's UIAbility instance has been created and the UIAbility is configured for singleton startup mode, calling the startAbility() method again to start the UIAbility instance will only go to the UIAbility's onNewWant() callback, and not to its onCreate() and onWindowStageCreate() lifecycle callbacks. The application can update the resources and data to be loaded in this callback for subsequent UI display.
Destroy state
The Destroy state is triggered when the UIAbility instance is destroyed. You can perform operations such as releasing system resources and saving data in the onDestroy() callback.