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'; 112import { hilog } from '@kit.PerformanceAnalysisKit'; 113import { BusinessError } from '@kit.BasicServicesKit'; 114 115const TAG: string = '[EntryAbility]'; 116const DOMAIN_NUMBER: number = 0xFF00; 117 118export default class EntryAbility extends UIAbility { 119 windowStage: window.WindowStage | undefined = undefined; 120 121 // ... 122 onWindowStageCreate(windowStage: window.WindowStage): void { 123 this.windowStage = windowStage; 124 // ... 125 } 126 127 onWindowStageDestroy() { 128 // Release UI resources. 129 // 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 onWindowStageDestroy() callback. 130 try { 131 if (this.windowStage) { 132 this.windowStage.off('windowStageEvent'); 133 } 134 } catch (err) { 135 let code = (err as BusinessError).code; 136 let message = (err as BusinessError).message; 137 hilog.error(DOMAIN_NUMBER, TAG, `Failed to disable the listener for windowStageEvent. Code is ${code}, message is ${message}`); 138 } 139 } 140} 141``` 142 143### WindowStageWillDestroy 144The [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. 145 146```ts 147import { UIAbility } from '@kit.AbilityKit'; 148import { window } from '@kit.ArkUI'; 149 150export default class EntryAbility extends UIAbility { 151 windowStage: window.WindowStage | undefined = undefined; 152 // ... 153 onWindowStageCreate(windowStage: window.WindowStage): void { 154 this.windowStage = windowStage; 155 // ... 156 } 157 onWindowStageWillDestroy(windowStage: window.WindowStage) { 158 // Release the resources obtained through the windowStage object. 159 } 160 onWindowStageDestroy() { 161 // Release UI resources. 162 } 163} 164``` 165 166> **NOTE** 167> 168> For details about how to use WindowStage, see [Window Development](../windowmanager/application-window-stage.md). 169 170 171### Foreground and Background 172 173The **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. 174 175The **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. 176 177The **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. 178 179For 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. 180 181When the application is switched to the background, you can disable location in the **onBackground()** callback to reduce system resource consumption. 182 183 184```ts 185import { UIAbility } from '@kit.AbilityKit'; 186 187export default class EntryAbility extends UIAbility { 188 // ... 189 190 onForeground(): void { 191 // Apply for the resources required by the system or re-apply for the resources released in onBackground(). 192 } 193 194 onBackground(): void { 195 // Release unused resources when the UI is invisible, or perform time-consuming operations in this callback, 196 // for example, saving the status. 197 } 198} 199``` 200 201Assume 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. 202 203```ts 204import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 205 206export default class EntryAbility extends UIAbility { 207 // ... 208 209 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { 210 // Update resources and data. 211 } 212} 213``` 214 215### Destroy 216 217The 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. 218 219The 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. 220<!--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--> 221 222```ts 223import { UIAbility } from '@kit.AbilityKit'; 224 225export default class EntryAbility extends UIAbility { 226 // ... 227 228 onDestroy() { 229 // Release system resources and save data. 230 } 231} 232``` 233