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