1# UIAbility Lifecycle 2 3 4## Overview 5 6When 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. 7 8The lifecycle of UIAbility has four states: **Create**, **Foreground**, **Background**, and **Destroy**, as shown in the figure below. 9 10**Figure 1** UIAbility lifecycle states 11 12 13 14 15## Description of Lifecycle States 16 17 18### Create 19 20The **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. 21 22 23```ts 24import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 25 26export default class EntryAbility extends UIAbility { 27 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 28 // Initialize the page. 29 } 30 // ... 31} 32``` 33 34> **NOTE** 35> 36> [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). 37 38### WindowStageCreate and WindowStageDestroy 39 40After 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. 41 42**Figure 2** WindowStageCreate and WindowStageDestroy 43 44 45 46In 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, switching to the foreground or background, or becoming interactive or non-interactive in the foreground. 47 48> **NOTE** 49> 50> The timing of the [WindowStage events](../reference/apis-arkui/js-apis-window.md#windowstageeventtype9) may vary according to the development scenario. 51 52```ts 53import { UIAbility } from '@kit.AbilityKit'; 54import { window } from '@kit.ArkUI'; 55import { hilog } from '@kit.PerformanceAnalysisKit'; 56 57const TAG: string = '[EntryAbility]'; 58const DOMAIN_NUMBER: number = 0xFF00; 59 60export default class EntryAbility extends UIAbility { 61 // ... 62 onWindowStageCreate(windowStage: window.WindowStage): void { 63 // Subscribe to the WindowStage events (having or losing focus, switching to the foreground or background, or becoming interactive or non-interactive in the foreground). 64 try { 65 windowStage.on('windowStageEvent', (data) => { 66 let stageEventType: window.WindowStageEventType = data; 67 switch (stageEventType) { 68 case window.WindowStageEventType.SHOWN: // Switch to the foreground. 69 hilog.info(DOMAIN_NUMBER, TAG, `windowStage foreground.`); 70 break; 71 case window.WindowStageEventType.ACTIVE: // Gain focus. 72 hilog.info(DOMAIN_NUMBER, TAG, `windowStage active.`); 73 break; 74 case window.WindowStageEventType.INACTIVE: // Lose focus. 75 hilog.info(DOMAIN_NUMBER, TAG, `windowStage inactive.`); 76 break; 77 case window.WindowStageEventType.HIDDEN: // Switch to the background. 78 hilog.info(DOMAIN_NUMBER, TAG, `windowStage background.`); 79 break; 80 case window.WindowStageEventType.RESUMED: // Interactive in the foreground. 81 hilog.info(DOMAIN_NUMBER, TAG, `windowStage resumed.`); 82 break; 83 case window.WindowStageEventType.PAUSED: // Non-interactive in the foreground. 84 hilog.info(DOMAIN_NUMBER, TAG, `windowStage paused.`); 85 break; 86 default: 87 break; 88 } 89 }); 90 } catch (exception) { 91 hilog.error(DOMAIN_NUMBER, TAG, 92 `Failed to enable the listener for window stage event changes. Cause: ${JSON.stringify(exception)}`); 93 } 94 hilog.info(DOMAIN_NUMBER, TAG, `%{public}s`, `Ability onWindowStageCreate`); 95 // Set the page to be loaded. 96 windowStage.loadContent('pages/Index', (err, data) => { 97 // ... 98 }); 99 } 100} 101``` 102 103> **NOTE** 104> 105> For details about how to use WindowStage, see [Window Development](../windowmanager/application-window-stage.md). 106 107Before 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. 108 109```ts 110import { UIAbility } from '@kit.AbilityKit'; 111import { window } from '@kit.ArkUI'; 112 113export default class EntryAbility extends UIAbility { 114 windowStage: window.WindowStage | undefined = undefined; 115 116 // ... 117 onWindowStageCreate(windowStage: window.WindowStage): void { 118 this.windowStage = windowStage; 119 // ... 120 } 121 122 onWindowStageDestroy() { 123 // Release UI resources. 124 } 125} 126``` 127 128### WindowStageWillDestroy 129The [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. 130 131```ts 132import { UIAbility } from '@kit.AbilityKit'; 133import { window } from '@kit.ArkUI'; 134import { BusinessError } from '@kit.BasicServicesKit'; 135import { hilog } from '@kit.PerformanceAnalysisKit'; 136 137const TAG: string = '[EntryAbility]'; 138const DOMAIN_NUMBER: number = 0xFF00; 139 140export default class EntryAbility extends UIAbility { 141 windowStage: window.WindowStage | undefined = undefined; 142 // ... 143 onWindowStageCreate(windowStage: window.WindowStage): void { 144 this.windowStage = windowStage; 145 // ... 146 } 147 148 onWindowStageWillDestroy(windowStage: window.WindowStage) { 149 // Release the resources obtained through the windowStage object. 150 // Unsubscribe from the WindowStage events (having or losing focus, switching to the foreground or background, or becoming interactive or non-interactive in the foreground) in the onWindowStageWillDestroy() callback. 151 try { 152 if (this.windowStage) { 153 this.windowStage.off('windowStageEvent'); 154 } 155 } catch (err) { 156 let code = (err as BusinessError).code; 157 let message = (err as BusinessError).message; 158 hilog.error(DOMAIN_NUMBER, TAG, `Failed to disable the listener for windowStageEvent. Code is ${code}, message is ${message}`); 159 } 160 } 161 162 onWindowStageDestroy() { 163 // Release UI resources. 164 } 165} 166``` 167 168> **NOTE** 169> 170> For details about how to use WindowStage, see [Window Development](../windowmanager/application-window-stage.md). 171 172 173### Foreground and Background 174 175The **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. 176 177The **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. 178 179The **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. 180 181For 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. 182 183When the application is switched to the background, you can disable location in the **onBackground()** callback to reduce system resource consumption. 184 185 186```ts 187import { UIAbility } from '@kit.AbilityKit'; 188 189export default class EntryAbility extends UIAbility { 190 // ... 191 192 onForeground(): void { 193 // Apply for the resources required by the system or re-apply for the resources released in onBackground(). 194 } 195 196 onBackground(): void { 197 // Release unused resources when the UI is invisible, or perform time-consuming operations in this callback, 198 // for example, saving the status. 199 } 200} 201``` 202 203Assume 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. 204 205```ts 206import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 207 208export default class EntryAbility extends UIAbility { 209 // ... 210 211 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { 212 // Update resources and data. 213 } 214} 215``` 216 217### Destroy 218 219The 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. 220 221The 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. 222<!--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--> 223 224```ts 225import { UIAbility } from '@kit.AbilityKit'; 226 227export default class EntryAbility extends UIAbility { 228 // ... 229 230 onDestroy() { 231 // Release system resources and save data. 232 } 233} 234``` 235