1e41f4b71Sopenharmony_ci# UIAbility组件生命周期
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## 概述
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci当用户打开、切换和返回到对应应用时,应用中的[UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md)实例会在其生命周期的不同状态之间转换。UIAbility类提供了一系列回调,通过这些回调可以知道当前UIAbility实例的某个状态发生改变,会经过UIAbility实例的创建和销毁,或者UIAbility实例发生了前后台的状态切换。
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciUIAbility的生命周期包括Create、Foreground、Background、Destroy四个状态,如下图所示。
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci**图1** UIAbility生命周期状态  
11e41f4b71Sopenharmony_ci![Ability-Life-Cycle](figures/Ability-Life-Cycle.png)  
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## 生命周期状态说明
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci### Create状态
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciCreate状态为在应用加载过程中,[UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md)实例创建完成时触发,系统会调用[onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate)回调。可以在该回调中进行页面初始化操作,例如变量定义资源加载等,用于后续的UI展示。
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci```ts
23e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
26e41f4b71Sopenharmony_ci  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
27e41f4b71Sopenharmony_ci    // 页面初始化
28e41f4b71Sopenharmony_ci  }
29e41f4b71Sopenharmony_ci  // ...
30e41f4b71Sopenharmony_ci}
31e41f4b71Sopenharmony_ci```
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci> **说明**:
34e41f4b71Sopenharmony_ci>
35e41f4b71Sopenharmony_ci> [Want](../reference/apis-ability-kit/js-apis-app-ability-want.md)是对象间信息传递的载体,可以用于应用组件间的信息传递。Want的详细介绍请参见[信息传递载体Want](want-overview.md)。
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci### WindowStageCreate和WindowStageDestroy状态
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci[UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md)实例创建完成之后,在进入Foreground之前,系统会创建一个WindowStage。WindowStage创建完成后会进入[onWindowStageCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate)回调,可以在该回调中设置UI加载、设置WindowStage的事件订阅。
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**图2** WindowStageCreate和WindowStageDestroy状态  
42e41f4b71Sopenharmony_ci![Ability-Life-Cycle-WindowStage](figures/Ability-Life-Cycle-WindowStage.png)  
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci在onWindowStageCreate()回调中通过[loadContent()](../reference/apis-arkui/js-apis-window.md#loadcontent9)方法设置应用要加载的页面,并根据需要调用[on('windowStageEvent')](../reference/apis-arkui/js-apis-window.md#onwindowstageevent9)方法订阅[WindowStage的事件](../reference/apis-arkui/js-apis-window.md#windowstageeventtype9)(获焦/失焦、切到前台/切到后台、前台可交互/前台不可交互)。
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci> **说明:**
47e41f4b71Sopenharmony_ci> 
48e41f4b71Sopenharmony_ci> 不同开发场景下[WindowStage事件](../reference/apis-arkui/js-apis-window.md#windowstageeventtype9)的时序可能存在差异。
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci```ts
51e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit';
52e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI';
53e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ciconst TAG: string = '[EntryAbility]';
56e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00;
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
59e41f4b71Sopenharmony_ci  // ...
60e41f4b71Sopenharmony_ci  onWindowStageCreate(windowStage: window.WindowStage): void {
61e41f4b71Sopenharmony_ci    // 设置WindowStage的事件订阅(获焦/失焦、切到前台/切到后台、前台可交互/前台不可交互)
62e41f4b71Sopenharmony_ci    try {
63e41f4b71Sopenharmony_ci      windowStage.on('windowStageEvent', (data) => {
64e41f4b71Sopenharmony_ci        let stageEventType: window.WindowStageEventType = data;
65e41f4b71Sopenharmony_ci        switch (stageEventType) {
66e41f4b71Sopenharmony_ci          case window.WindowStageEventType.SHOWN: // 切到前台
67e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, `windowStage foreground.`);
68e41f4b71Sopenharmony_ci            break;
69e41f4b71Sopenharmony_ci          case window.WindowStageEventType.ACTIVE: // 获焦状态
70e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, `windowStage active.`);
71e41f4b71Sopenharmony_ci            break;
72e41f4b71Sopenharmony_ci          case window.WindowStageEventType.INACTIVE: // 失焦状态
73e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, `windowStage inactive.`);
74e41f4b71Sopenharmony_ci            break;
75e41f4b71Sopenharmony_ci          case window.WindowStageEventType.HIDDEN: // 切到后台
76e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, `windowStage background.`);
77e41f4b71Sopenharmony_ci            break;
78e41f4b71Sopenharmony_ci          case window.WindowStageEventType.RESUMED: // 前台可交互状态
79e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, `windowStage resumed.`);
80e41f4b71Sopenharmony_ci            break;
81e41f4b71Sopenharmony_ci          case window.WindowStageEventType.PAUSED: // 前台不可交互状态
82e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, `windowStage paused.`);
83e41f4b71Sopenharmony_ci            break;
84e41f4b71Sopenharmony_ci          default:
85e41f4b71Sopenharmony_ci            break;
86e41f4b71Sopenharmony_ci        }
87e41f4b71Sopenharmony_ci      });
88e41f4b71Sopenharmony_ci    } catch (exception) {
89e41f4b71Sopenharmony_ci      hilog.error(DOMAIN_NUMBER, TAG,
90e41f4b71Sopenharmony_ci        `Failed to enable the listener for window stage event changes. Cause: ${JSON.stringify(exception)}`);
91e41f4b71Sopenharmony_ci    }
92e41f4b71Sopenharmony_ci    hilog.info(DOMAIN_NUMBER, TAG, `%{public}s`, `Ability onWindowStageCreate`);
93e41f4b71Sopenharmony_ci    // 设置UI加载
94e41f4b71Sopenharmony_ci    windowStage.loadContent('pages/Index', (err, data) => {
95e41f4b71Sopenharmony_ci      // ...
96e41f4b71Sopenharmony_ci    });
97e41f4b71Sopenharmony_ci  }
98e41f4b71Sopenharmony_ci}
99e41f4b71Sopenharmony_ci```
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci> **说明:**
102e41f4b71Sopenharmony_ci>
103e41f4b71Sopenharmony_ci> WindowStage的相关使用请参见[窗口开发指导](../windowmanager/application-window-stage.md)。
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci对应于[onWindowStageCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate)回调。在[UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md)实例销毁之前,则会先进入[onWindowStageDestroy()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagedestroy)回调,可以在该回调中释放UI资源。
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci```ts
108e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit';
109e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI';
110e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
111e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ciconst TAG: string = '[EntryAbility]';
114e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00;
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
117e41f4b71Sopenharmony_ci  windowStage: window.WindowStage | undefined = undefined;
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci  // ...
120e41f4b71Sopenharmony_ci  onWindowStageCreate(windowStage: window.WindowStage): void {
121e41f4b71Sopenharmony_ci    this.windowStage = windowStage;
122e41f4b71Sopenharmony_ci    // ...
123e41f4b71Sopenharmony_ci  }
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci  onWindowStageDestroy() {
126e41f4b71Sopenharmony_ci    // 释放UI资源
127e41f4b71Sopenharmony_ci    // 例如在onWindowStageDestroy()中注销WindowStage事件订阅(获焦/失焦、切到前台/切到后台、前台可交互/前台不可交互)
128e41f4b71Sopenharmony_ci    try {
129e41f4b71Sopenharmony_ci      if (this.windowStage) {
130e41f4b71Sopenharmony_ci        this.windowStage.off('windowStageEvent');
131e41f4b71Sopenharmony_ci      }
132e41f4b71Sopenharmony_ci    } catch (err) {
133e41f4b71Sopenharmony_ci      let code = (err as BusinessError).code;
134e41f4b71Sopenharmony_ci      let message = (err as BusinessError).message;
135e41f4b71Sopenharmony_ci      hilog.error(DOMAIN_NUMBER, TAG, `Failed to disable the listener for windowStageEvent. Code is ${code}, message is ${message}`);
136e41f4b71Sopenharmony_ci    }
137e41f4b71Sopenharmony_ci  }
138e41f4b71Sopenharmony_ci}
139e41f4b71Sopenharmony_ci```
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci### WindowStageWillDestroy状态
142e41f4b71Sopenharmony_ci对应[onWindowStageWillDestroy()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagewilldestroy12)回调,在WindowStage销毁前执行,此时WindowStage可以使用。
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci```ts
145e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit';
146e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI';
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
149e41f4b71Sopenharmony_ci  windowStage: window.WindowStage | undefined = undefined;
150e41f4b71Sopenharmony_ci  // ...
151e41f4b71Sopenharmony_ci  onWindowStageCreate(windowStage: window.WindowStage): void {
152e41f4b71Sopenharmony_ci    this.windowStage = windowStage;
153e41f4b71Sopenharmony_ci    // ...
154e41f4b71Sopenharmony_ci  }
155e41f4b71Sopenharmony_ci  onWindowStageWillDestroy(windowStage: window.WindowStage) {
156e41f4b71Sopenharmony_ci    // 释放通过windowStage对象获取的资源
157e41f4b71Sopenharmony_ci  }
158e41f4b71Sopenharmony_ci  onWindowStageDestroy() {
159e41f4b71Sopenharmony_ci    // 释放UI资源
160e41f4b71Sopenharmony_ci  }
161e41f4b71Sopenharmony_ci}
162e41f4b71Sopenharmony_ci```
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci> **说明:**
165e41f4b71Sopenharmony_ci>
166e41f4b71Sopenharmony_ci> WindowStage的相关使用请参见[窗口开发指导](../windowmanager/application-window-stage.md)。
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci### Foreground和Background状态
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ciForeground和Background状态分别在[UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md)实例切换至前台和切换至后台时触发,对应于[onForeground()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonforeground)回调和[onBackground()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonbackground)回调。
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci`onForeground()`回调,在UIAbility的UI可见之前,如UIAbility切换至前台时触发。可以在`onForeground()`回调中申请系统需要的资源,或者重新申请在`onBackground()`中释放的资源。
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci`onBackground()`回调,在UIAbility的UI完全不可见之后,如UIAbility切换至后台时候触发。可以在`onBackground()`回调中释放UI不可见时无用的资源,或者在此回调中执行较为耗时的操作,例如状态保存等。
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci例如应用在使用过程中需要使用用户定位时,假设应用已获得用户的定位权限授权。在UI显示之前,可以在`onForeground()`回调中开启定位功能,从而获取到当前的位置信息。
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci当应用切换到后台状态,可以在`onBackground()`回调中停止定位功能,以节省系统的资源消耗。
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci```ts
183e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit';
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
186e41f4b71Sopenharmony_ci  // ...
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci  onForeground(): void {
189e41f4b71Sopenharmony_ci    // 申请系统需要的资源,或者重新申请在onBackground()中释放的资源
190e41f4b71Sopenharmony_ci  }
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci  onBackground(): void {
193e41f4b71Sopenharmony_ci    // 释放UI不可见时无用的资源,或者在此回调中执行较为耗时的操作
194e41f4b71Sopenharmony_ci    // 例如状态保存等
195e41f4b71Sopenharmony_ci  }
196e41f4b71Sopenharmony_ci}
197e41f4b71Sopenharmony_ci```
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci当应用的UIAbility实例已创建,且UIAbility配置为[singleton](uiability-launch-type.md#singleton启动模式)启动模式时,再次调用[startAbility()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability)方法启动该UIAbility实例时,只会进入该UIAbility的[onNewWant()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonnewwant)回调,不会进入其[onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate)和[onWindowStageCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate)生命周期回调。应用可以在该回调中更新要加载的资源和数据等,用于后续的UI展示。
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci```ts
202e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
205e41f4b71Sopenharmony_ci  // ...
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {
208e41f4b71Sopenharmony_ci    // 更新资源、数据
209e41f4b71Sopenharmony_ci  }
210e41f4b71Sopenharmony_ci}
211e41f4b71Sopenharmony_ci```
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci### Destroy状态
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ciDestroy状态在[UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md)实例销毁时触发。可以在onDestroy()回调中进行系统资源的释放、数据的保存等操作。
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci例如,调用[terminateSelf()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself)方法停止当前UIAbility实例,执行onDestroy()回调,并完成UIAbility实例的销毁。
218e41f4b71Sopenharmony_ci<!--RP1-->再比如,用户使用最近任务列表关闭该UIAbility实例,执行onDestroy()回调,并完成UIAbility实例的销毁。<!--RP1End-->
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci```ts
221e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit';
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
224e41f4b71Sopenharmony_ci  // ...
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci  onDestroy() {
227e41f4b71Sopenharmony_ci    // 系统资源的释放、数据的保存等
228e41f4b71Sopenharmony_ci  }
229e41f4b71Sopenharmony_ci}
230e41f4b71Sopenharmony_ci```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci## 相关实例
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci针对UIAbility生命周期,有以下相关实例可供参考:
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci- [UIAbility和自定义组件生命周期(ArkTS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/Ability/UIAbilityLifeCycle)