1# UIAbility Lifecycle 2 3 4## Overview 5 6When a user opens or switches to and from an application, the UIAbility 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 instance is created during application loading. It corresponds to the **onCreate()** callback. In this callback, you can perform application initialization operations, for example, defining variables or loading resources. 21 22 23```ts 24import UIAbility from '@ohos.app.ability.UIAbility'; 25import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 26import Want from '@ohos.app.ability.Want'; 27 28export default class EntryAbility extends UIAbility { 29 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 30 // Initialize the application. 31 } 32 // ... 33} 34``` 35 36> **NOTE** 37> 38> [Want](../reference/apis/js-apis-app-ability-want.md) is used as the carrier to transfer information between application components. For details, see [Want](want-overview.md). 39 40### WindowStageCreate and WindowStageDestory 41 42After the UIAbility instance is created but before it enters the **Foreground** state, the system creates a WindowStage instance and triggers the **onWindowStageCreate()** callback. You can set UI loading and WindowStage event subscription in the callback. 43 44**Figure 2** WindowStageCreate and WindowStageDestory 45 46 47 48In the **onWindowStageCreate()** callback, use [loadContent()](../reference/apis/js-apis-window.md#loadcontent9-2) to set the page to be loaded, and call [on('windowStageEvent')](../reference/apis/js-apis-window.md#onwindowstageevent9) to subscribe to [WindowStage events](../reference/apis/js-apis-window.md#windowstageeventtype9), for example, having or losing focus, or becoming visible or invisible. 49 50```ts 51import UIAbility from '@ohos.app.ability.UIAbility'; 52import window from '@ohos.window'; 53 54export default class EntryAbility extends UIAbility { 55 // ... 56 57 onWindowStageCreate(windowStage: window.WindowStage) { 58 // Subscribe to the WindowStage events (having or losing focus, or becoming visible or invisible). 59 try { 60 windowStage.on('windowStageEvent', (data) => { 61 let stageEventType: window.WindowStageEventType = data; 62 switch (stageEventType) { 63 case window.WindowStageEventType.SHOWN: // Switch to the foreground. 64 console.info('windowStage foreground.'); 65 break; 66 case window.WindowStageEventType.ACTIVE: // Gain focus. 67 console.info('windowStage active.'); 68 break; 69 case window.WindowStageEventType.INACTIVE: // Lose focus. 70 console.info('windowStage inactive.'); 71 break; 72 case window.WindowStageEventType.HIDDEN: // Switch to the background. 73 console.info('windowStage background.'); 74 break; 75 default: 76 break; 77 } 78 }); 79 } catch (exception) { 80 console.error('Failed to enable the listener for window stage event changes. Cause:' + 81 JSON.stringify(exception)); 82 } 83 84 // Set the page to be loaded. 85 windowStage.loadContent('pages/Index', (err, data) => { 86 // ... 87 }); 88 } 89} 90``` 91 92> **NOTE** 93> 94> For details about how to use WindowStage, see [Window Development](../windowmanager/application-window-stage.md). 95 96Before the UIAbility instance is destroyed, the **onWindowStageDestroy()** callback is invoked to release UI resources. In this callback, you can unsubscribe from the WindowStage events. 97 98 99```ts 100import UIAbility from '@ohos.app.ability.UIAbility'; 101import window from '@ohos.window'; 102import { BusinessError } from '@ohos.base'; 103 104export default class EntryAbility extends UIAbility { 105 windowStage: window.WindowStage | undefined = undefined; 106 // ... 107 108 onWindowStageCreate(windowStage: window.WindowStage) { 109 this.windowStage = windowStage; 110 // ... 111 } 112 113 onWindowStageDestroy() { 114 // Release UIresources. 115 // Unsubscribe from the WindowStage events such as having or losing focus in the onWindowStageDestroy() callback. 116 try { 117 if (this.windowStage) { 118 this.windowStage.off('windowStageEvent'); 119 } 120 } catch (err) { 121 let code = (err as BusinessError).code; 122 let message = (err as BusinessError).message; 123 console.error(`Failed to disable the listener for windowStageEvent. Code is ${code}, message is ${message}`); 124 }; 125 } 126} 127``` 128 129 130### Foreground and Background 131 132The **Foreground** and **Background** states are triggered when the UIAbility instance is switched to the foreground and background, respectively. They correspond to the **onForeground()** and **onBackground()** callbacks. 133 134The **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. 135 136The **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. 137 138For 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. 139 140When the application is switched to the background, you can disable location in the **onBackground()** callback to reduce system resource consumption. 141 142 143```ts 144import UIAbility from '@ohos.app.ability.UIAbility'; 145 146export default class EntryAbility extends UIAbility { 147 // ... 148 149 onForeground() { 150 // Apply for the resources required by the system or re-apply for the resources released in onBackground(). 151 } 152 153 onBackground() { 154 // Release unused resources when the UI is invisible, or perform time-consuming operations in this callback, 155 // for example, saving the status. 156 } 157} 158``` 159 160 161### Destroy 162 163The **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. 164 165The UIAbility instance is destroyed when **terminateSelf()** is called or the user closes the instance in the system application Recents. 166 167```ts 168import UIAbility from '@ohos.app.ability.UIAbility'; 169 170export default class EntryAbility extends UIAbility { 171 // ... 172 173 onDestroy() { 174 // Release system resources and save data. 175 } 176} 177``` 178