1e41f4b71Sopenharmony_ci# UIAbility Lifecycle 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciWhen a user opens or switches to and from an application, the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) instances in the application transit in their different states. The UIAbility class provides a series of callbacks. Through these callbacks, you can know the state changes of the UIAbility instance. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThe lifecycle of UIAbility has four states: **Create**, **Foreground**, **Background**, and **Destroy**, as shown in the figure below. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci**Figure 1** UIAbility lifecycle states 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Description of Lifecycle States 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci### Create 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciThe **Create** state is triggered when the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) instance is created during application loading. It corresponds to the [onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate) callback. In this callback, you can perform page initialization operations, for example, defining variables or loading resources. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci```ts 24e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 27e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 28e41f4b71Sopenharmony_ci // Initialize the page. 29e41f4b71Sopenharmony_ci } 30e41f4b71Sopenharmony_ci // ... 31e41f4b71Sopenharmony_ci} 32e41f4b71Sopenharmony_ci``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci> **NOTE** 35e41f4b71Sopenharmony_ci> 36e41f4b71Sopenharmony_ci> [Want](../reference/apis-ability-kit/js-apis-app-ability-want.md) is used as the carrier to transfer information between application components. For details, see [Want](want-overview.md). 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci### WindowStageCreate and WindowStageDestroy 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciAfter the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) instance is created but before it enters the Foreground state, the system creates a WindowStage instance and triggers the [onWindowStageCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) callback. You can set UI loading and WindowStage event subscription in the callback. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**Figure 2** WindowStageCreate and WindowStageDestroy 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ciIn the **onWindowStageCreate()** callback, use [loadContent()](../reference/apis-arkui/js-apis-window.md#loadcontent9) to set the page to be loaded, and call [on('windowStageEvent')](../reference/apis-arkui/js-apis-window.md#onwindowstageevent9) to subscribe to [WindowStage events](../reference/apis-arkui/js-apis-window.md#windowstageeventtype9), for example, having or losing focus, or becoming visible or invisible. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci```ts 49e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 50e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 51e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ciconst TAG: string = '[EntryAbility]'; 54e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00; 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 57e41f4b71Sopenharmony_ci // ... 58e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 59e41f4b71Sopenharmony_ci // Subscribe to the WindowStage events (having or losing focus, or becoming visible or invisible). 60e41f4b71Sopenharmony_ci try { 61e41f4b71Sopenharmony_ci windowStage.on('windowStageEvent', (data) => { 62e41f4b71Sopenharmony_ci let stageEventType: window.WindowStageEventType = data; 63e41f4b71Sopenharmony_ci switch (stageEventType) { 64e41f4b71Sopenharmony_ci case window.WindowStageEventType.SHOWN: // Switch to the foreground. 65e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'windowStage foreground.'); 66e41f4b71Sopenharmony_ci break; 67e41f4b71Sopenharmony_ci case window.WindowStageEventType.ACTIVE: // Gain focus. 68e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'windowStage active.'); 69e41f4b71Sopenharmony_ci break; 70e41f4b71Sopenharmony_ci case window.WindowStageEventType.INACTIVE: // Lose focus. 71e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'windowStage inactive.'); 72e41f4b71Sopenharmony_ci break; 73e41f4b71Sopenharmony_ci case window.WindowStageEventType.HIDDEN: // Switch to the background. 74e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'windowStage background.'); 75e41f4b71Sopenharmony_ci break; 76e41f4b71Sopenharmony_ci default: 77e41f4b71Sopenharmony_ci break; 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci }); 80e41f4b71Sopenharmony_ci } catch (exception) { 81e41f4b71Sopenharmony_ci hilog.error(DOMAIN_NUMBER, TAG, 'Failed to enable the listener for window stage event changes. Cause:' + JSON.stringify(exception)); 82e41f4b71Sopenharmony_ci } 83e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '%{public}s', 'Ability onWindowStageCreate'); 84e41f4b71Sopenharmony_ci // Set the page to be loaded. 85e41f4b71Sopenharmony_ci windowStage.loadContent('pages/Index', (err, data) => { 86e41f4b71Sopenharmony_ci // ... 87e41f4b71Sopenharmony_ci }); 88e41f4b71Sopenharmony_ci } 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci``` 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci> **NOTE** 93e41f4b71Sopenharmony_ci> 94e41f4b71Sopenharmony_ci> For details about how to use WindowStage, see [Window Development](../windowmanager/application-window-stage.md). 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciBefore the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) instance is destroyed, the [onWindowStageDestroy()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagedestroy) callback is invoked to release UI resources. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci```ts 99e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 100e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 101e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 102e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ciconst TAG: string = '[EntryAbility]'; 105e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00; 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 108e41f4b71Sopenharmony_ci windowStage: window.WindowStage | undefined = undefined; 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci // ... 111e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 112e41f4b71Sopenharmony_ci this.windowStage = windowStage; 113e41f4b71Sopenharmony_ci // ... 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci onWindowStageDestroy() { 117e41f4b71Sopenharmony_ci // Release UI resources. 118e41f4b71Sopenharmony_ci // Unsubscribe from the WindowStage events such as having or losing focus in the onWindowStageDestroy() callback. 119e41f4b71Sopenharmony_ci try { 120e41f4b71Sopenharmony_ci if (this.windowStage) { 121e41f4b71Sopenharmony_ci this.windowStage.off('windowStageEvent'); 122e41f4b71Sopenharmony_ci } 123e41f4b71Sopenharmony_ci } catch (err) { 124e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 125e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 126e41f4b71Sopenharmony_ci hilog.error(DOMAIN_NUMBER, TAG, `Failed to disable the listener for windowStageEvent. Code is ${code}, message is ${message}`); 127e41f4b71Sopenharmony_ci } 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci} 130e41f4b71Sopenharmony_ci``` 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci### WindowStageWillDestroy 133e41f4b71Sopenharmony_ciThe [onWindowStageWillDestroy()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagewilldestroy12) callback is invoked before the window stage is destroyed. In this case, the window stage can still be used. 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci```ts 136e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 137e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 140e41f4b71Sopenharmony_ci windowStage: window.WindowStage | undefined = undefined; 141e41f4b71Sopenharmony_ci // ... 142e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 143e41f4b71Sopenharmony_ci this.windowStage = windowStage; 144e41f4b71Sopenharmony_ci // ... 145e41f4b71Sopenharmony_ci } 146e41f4b71Sopenharmony_ci onWindowStageWillDestroy(windowStage: window.WindowStage) { 147e41f4b71Sopenharmony_ci // Release the resources obtained through the windowStage object. 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci onWindowStageDestroy() { 150e41f4b71Sopenharmony_ci // Release UI resources. 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci} 153e41f4b71Sopenharmony_ci``` 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci> **NOTE** 156e41f4b71Sopenharmony_ci> 157e41f4b71Sopenharmony_ci> For details about how to use WindowStage, see [Window Development](../windowmanager/application-window-stage.md). 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci### Foreground and Background 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ciThe **Foreground** and **Background** states are triggered when the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) instance is switched to the foreground and background, respectively. They correspond to the [onForeground()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonforeground) and [onBackground()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonbackground) callbacks. 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ciThe **onForeground()** callback is triggered when the UI of the UIAbility instance is about to become visible, for example, when the UIAbility instance is about to enter the foreground. In this callback, you can apply for resources required by the system or re-apply for resources that have been released in the **onBackground()** callback. 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciThe **onBackground()** callback is triggered when the UI of the UIAbility instance is about to become invisible, for example, when the UIAbility instance is about to enter the background. In this callback, you can release unused resources or perform time-consuming operations such as saving the status. 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ciFor example, there is an application that requires location access and has obtained the location permission from the user. Before the UI is displayed, you can enable location in the **onForeground()** callback to obtain the location information. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ciWhen the application is switched to the background, you can disable location in the **onBackground()** callback to reduce system resource consumption. 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci```ts 174e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 177e41f4b71Sopenharmony_ci // ... 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci onForeground(): void { 180e41f4b71Sopenharmony_ci // Apply for the resources required by the system or re-apply for the resources released in onBackground(). 181e41f4b71Sopenharmony_ci } 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci onBackground(): void { 184e41f4b71Sopenharmony_ci // Release unused resources when the UI is invisible, or perform time-consuming operations in this callback, 185e41f4b71Sopenharmony_ci // for example, saving the status. 186e41f4b71Sopenharmony_ci } 187e41f4b71Sopenharmony_ci} 188e41f4b71Sopenharmony_ci``` 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ciAssume that the application already has a UIAbility instance created, and the launch type of the UIAbility instance is set to [singleton](uiability-launch-type.md#singleton). If [startAbility()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called again to start the UIAbility instance, the [onNewWant()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonnewwant) callback is invoked, but the [onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate) and [onWindowStageCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) callbacks are not. The application can update the resources and data to be loaded in the callback, which will be used for UI display. 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci```ts 193e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 196e41f4b71Sopenharmony_ci // ... 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { 199e41f4b71Sopenharmony_ci // Update resources and data. 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci} 202e41f4b71Sopenharmony_ci``` 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci### Destroy 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ciThe Destroy state is triggered when the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) instance is destroyed. You can perform operations such as releasing system resources and saving data in the **onDestroy()** callback. 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ciThe UIAbility instance is destroyed when [terminateSelf()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself) is called and the **onDestroy()** callback is invoked. 209e41f4b71Sopenharmony_ci<!--RP1-->The UIAbility instance is also destroyed when the user closes the instance in the system application Recents and the **onDestroy()** callback is invoked.<!--RP1End--> 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci```ts 212e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 215e41f4b71Sopenharmony_ci // ... 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci onDestroy() { 218e41f4b71Sopenharmony_ci // Release system resources and save data. 219e41f4b71Sopenharmony_ci } 220e41f4b71Sopenharmony_ci} 221e41f4b71Sopenharmony_ci``` 222