1# @ohos.app.ability.abilityLifecycleCallback (UIAbility Lifecycle Callback Listener) 2<!--Kit: Ability Kit--> 3<!--Subsystem: Ability--> 4<!--Owner: @wkljy; @xuzhihao666--> 5<!--Designer: @ccllee1--> 6<!--Tester: @lixueqing513--> 7<!--Adviser: @huipeizi--> 8 9The lifecycle of a [UIAbility](js-apis-app-ability-uiAbility.md) dynamically changes from creation to destruction. The AbilityLifecycleCallback module provides the capability to listen for these lifecycle changes, which can be used for scenarios such as tracking the runtime duration of each UIAbility and performing data loading decoupled from the service logic of UIAbility. 10 11> **NOTE** 12> 13> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 14> 15> The APIs of this module can be used only in the stage model. 16> 17> The APIs provided by this module can listen for lifecycle changes of the UIAbility within the same process. 18 19## How to Use 20 211. An application creates an AbilityLifecycleCallback object and calls [ApplicationContext.on('abilityLifecycle')](js-apis-inner-application-applicationContext.md#applicationcontextonabilitylifecycle) to register a listener to listen for UIAbility lifecycle changes. 222. When the UIAbility lifecycle changes, the application receives a notification indicating the UIAbility lifecycle change through the registered AbilityLifecycleCallback object. 233. When the application does not need to listen for the UIAbility lifecycle changes, the application can unregister the listener by calling [ApplicationContext.off('abilityLifecycle')](js-apis-inner-application-applicationContext.md#applicationcontextoffabilitylifecycle). 24 25## Modules to Import 26 27```ts 28import { AbilityLifecycleCallback } from '@kit.AbilityKit'; 29``` 30 31## AbilityLifecycleCallback 32 33### onAbilityCreate 34 35onAbilityCreate(ability: UIAbility): void 36 37Called after the [onCreate](js-apis-app-ability-uiAbility.md#oncreate) callback of the UIAbility is triggered. 38 39**Atomic service API**: This API can be used in atomic services since API version 11. 40 41**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 42 43**Parameters** 44 45 | Name| Type| Mandatory| Description| 46 | -------- | -------- | -------- | -------- | 47 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 48 49**Example** 50 51For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 52 53### onWindowStageCreate 54 55onWindowStageCreate(ability: UIAbility, windowStage: window.WindowStage): void 56 57Called after the [onWindowStageCreate](js-apis-app-ability-uiAbility.md#onwindowstagecreate) callback of the UIAbility is triggered. 58 59**Atomic service API**: This API can be used in atomic services since API version 11. 60 61**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 62 63**Parameters** 64 65 | Name| Type| Mandatory| Description| 66 | -------- | -------- | -------- | -------- | 67 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 68 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 69 70**Example** 71 72For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 73 74### onWindowStageActive 75 76onWindowStageActive(ability: UIAbility, windowStage: window.WindowStage): void 77 78Called when the main window of the UIAbility gains focus. 79 80**Atomic service API**: This API can be used in atomic services since API version 11. 81 82**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 83 84**Parameters** 85 86 | Name| Type| Mandatory| Description| 87 | -------- | -------- | -------- | -------- | 88 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 89 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 90 91**Example** 92 93For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 94 95### onWindowStageInactive 96 97onWindowStageInactive(ability: UIAbility, windowStage: window.WindowStage): void 98 99Called when the main window of the UIAbility loses focus. 100 101**Atomic service API**: This API can be used in atomic services since API version 11. 102 103**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 104 105**Parameters** 106 107 | Name| Type| Mandatory| Description| 108 | -------- | -------- | -------- | -------- | 109 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 110 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 111 112**Example** 113 114For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 115 116### onWindowStageDestroy 117 118onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void 119 120Called after the [onWindowStageDestroy](js-apis-app-ability-uiAbility.md#onwindowstagedestroy) callback of the UIAbility is triggered. 121 122**Atomic service API**: This API can be used in atomic services since API version 11. 123 124**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 125 126**Parameters** 127 128 | Name| Type| Mandatory| Description| 129 | -------- | -------- | -------- | -------- | 130 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 131 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 132 133**Example** 134 135For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 136 137### onAbilityDestroy 138 139onAbilityDestroy(ability: UIAbility): void 140 141Called after the [onDestroy](js-apis-app-ability-uiAbility.md#ondestroy) callback of the UIAbility is triggered. 142 143**Atomic service API**: This API can be used in atomic services since API version 11. 144 145**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 146 147**Parameters** 148 149 | Name| Type| Mandatory| Description| 150 | -------- | -------- | -------- | -------- | 151 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 152 153**Example** 154 155For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 156 157### onAbilityForeground 158 159onAbilityForeground(ability: UIAbility): void 160 161Called after the [onForeground](js-apis-app-ability-uiAbility.md#onforeground) callback of the UIAbility is triggered. 162 163**Atomic service API**: This API can be used in atomic services since API version 11. 164 165**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 166 167**Parameters** 168 169 | Name| Type| Mandatory| Description| 170 | -------- | -------- | -------- | -------- | 171 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 172 173**Example** 174 175For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 176 177### onAbilityBackground 178 179onAbilityBackground(ability: UIAbility): void 180 181Called after the [onBackground](js-apis-app-ability-uiAbility.md#onbackground) callback of the UIAbility is triggered. 182 183**Atomic service API**: This API can be used in atomic services since API version 11. 184 185**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 186 187**Parameters** 188 189 | Name| Type| Mandatory| Description| 190 | -------- | -------- | -------- | -------- | 191 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 192 193**Example** 194 195For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 196 197### onAbilityContinue 198 199onAbilityContinue(ability: UIAbility): void 200 201Called after the [onContinue](js-apis-app-ability-uiAbility.md#oncontinue) callback of the UIAbility is triggered. 202 203**Atomic service API**: This API can be used in atomic services since API version 11. 204 205**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 206 207**Parameters** 208 209 | Name| Type| Mandatory| Description| 210 | -------- | -------- | -------- | -------- | 211 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 212 213**Example** 214 215For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 216 217### onAbilityWillCreate<sup>12+</sup> 218 219onAbilityWillCreate?(ability: UIAbility): void 220 221Called before the [onCreate](js-apis-app-ability-uiAbility.md#oncreate) callback of the UIAbility is triggered. 222 223**Atomic service API**: This API can be used in atomic services since API version 12. 224 225**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 226 227**Parameters** 228 229 | Name| Type| Mandatory| Description| 230 | -------- | -------- | -------- | -------- | 231 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 232 233**Example** 234 235For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 236 237### onWindowStageWillCreate<sup>12+</sup> 238 239onWindowStageWillCreate?(ability: UIAbility, windowStage: window.WindowStage): void 240 241Called before the [onWindowStageCreate](js-apis-app-ability-uiAbility.md#onwindowstagecreate) callback of the UIAbility is triggered. 242 243**Atomic service API**: This API can be used in atomic services since API version 12. 244 245**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 246 247**Parameters** 248 249 | Name| Type| Mandatory| Description| 250 | -------- | -------- | -------- | -------- | 251 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 252 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 253 254**Example** 255 256For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 257 258### onWindowStageWillDestroy<sup>12+</sup> 259 260onWindowStageWillDestroy?(ability: UIAbility, windowStage: window.WindowStage): void 261 262Called before the [onWindowStageDestroy](js-apis-app-ability-uiAbility.md#onwindowstagedestroy) callback of the UIAbility is triggered. 263 264**Atomic service API**: This API can be used in atomic services since API version 12. 265 266**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 267 268**Parameters** 269 270 | Name| Type| Mandatory| Description| 271 | -------- | -------- | -------- | -------- | 272 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 273 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 274 275**Example** 276 277For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 278 279### onAbilityWillForeground<sup>12+</sup> 280 281onAbilityWillForeground?(ability: UIAbility): void 282 283Called before the [onForeground](js-apis-app-ability-uiAbility.md#onforeground) callback of the UIAbility is triggered. 284 285**Atomic service API**: This API can be used in atomic services since API version 12. 286 287**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 288 289**Parameters** 290 291 | Name| Type| Mandatory| Description| 292 | -------- | -------- | -------- | -------- | 293 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 294 295**Example** 296 297For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 298 299### onAbilityWillDestroy<sup>12+</sup> 300 301onAbilityWillDestroy?(ability: UIAbility): void 302 303Called before the [onDestroy](js-apis-app-ability-uiAbility.md#ondestroy) callback of the UIAbility is triggered. 304 305**Atomic service API**: This API can be used in atomic services since API version 12. 306 307**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 308 309**Parameters** 310 311 | Name| Type| Mandatory| Description| 312 | -------- | -------- | -------- | -------- | 313 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 314 315**Example** 316 317For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 318 319### onAbilityWillBackground<sup>12+</sup> 320 321onAbilityWillBackground?(ability: UIAbility): void 322 323Called before the [onBackground](js-apis-app-ability-uiAbility.md#onbackground) callback of the UIAbility is triggered. 324 325**Atomic service API**: This API can be used in atomic services since API version 12. 326 327**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 328 329**Parameters** 330 331 | Name| Type| Mandatory| Description| 332 | -------- | -------- | -------- | -------- | 333 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 334 335**Example** 336 337For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 338 339### onWillNewWant<sup>12+</sup> 340 341onWillNewWant?(ability: UIAbility): void 342 343Called before the [onNewWant](js-apis-app-ability-uiAbility.md#onnewwant) callback of the UIAbility is triggered. 344 345**Atomic service API**: This API can be used in atomic services since API version 12. 346 347**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 348 349**Parameters** 350 351 | Name| Type| Mandatory| Description| 352 | -------- | -------- | -------- | -------- | 353 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 354 355**Example** 356 357For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 358 359### onNewWant<sup>12+</sup> 360 361onNewWant?(ability: UIAbility): void 362 363Called after the [onNewWant](js-apis-app-ability-uiAbility.md#onnewwant) callback of the UIAbility is triggered. 364 365**Atomic service API**: This API can be used in atomic services since API version 12. 366 367**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 368 369**Parameters** 370 371 | Name| Type| Mandatory| Description| 372 | -------- | -------- | -------- | -------- | 373 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 374 375**Example** 376 377For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 378 379### onAbilityWillContinue<sup>12+</sup> 380 381onAbilityWillContinue?(ability: UIAbility): void 382 383Called before the [onContinue](js-apis-app-ability-uiAbility.md#oncontinue) callback of the UIAbility is triggered. 384 385**Atomic service API**: This API can be used in atomic services since API version 12. 386 387**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 388 389**Parameters** 390 391 | Name| Type| Mandatory| Description| 392 | ------ | ------ | ------ | ------ | 393 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 394 395**Example** 396 397For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 398 399### onWindowStageWillRestore<sup>12+</sup> 400 401onWindowStageWillRestore?(ability: UIAbility, windowStage: window.WindowStage): void 402 403Called before the [onWindowStageRestore](js-apis-app-ability-uiAbility.md#onwindowstagerestore) callback of the UIAbility is triggered. 404 405**Atomic service API**: This API can be used in atomic services since API version 12. 406 407**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 408 409**Parameters** 410 411 | Name| Type| Mandatory| Description| 412 | ------ | ------ | ------ | ------ | 413 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 414 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 415 416**Example** 417 418For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 419 420### onWindowStageRestore<sup>12+</sup> 421 422onWindowStageRestore?(ability: UIAbility, windowStage: window.WindowStage): void 423 424Called after the [onWindowStageRestore](js-apis-app-ability-uiAbility.md#onwindowstagerestore) callback of the UIAbility is triggered. 425 426**Atomic service API**: This API can be used in atomic services since API version 12. 427 428**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 429 430**Parameters** 431 432 | Name| Type| Mandatory| Description| 433 | ------ | ------ | ------ | ------ | 434 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 435 | windowStage | [window.WindowStage](../apis-arkui/arkts-apis-window-WindowStage.md) | Yes| Main window manager of the UIAbility associated with the callback event.| 436 437**Example** 438 439For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 440 441### onAbilityWillSaveState<sup>12+</sup> 442 443onAbilityWillSaveState?(ability: UIAbility): void 444 445Called before the [onSaveState](js-apis-app-ability-uiAbility.md#onsavestate) callback of the UIAbility is triggered. 446 447**Atomic service API**: This API can be used in atomic services since API version 12. 448 449**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 450 451**Parameters** 452 453 | Name| Type| Mandatory| Description| 454 | ------ | ------ | ------ | ------ | 455 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 456 457**Example** 458 459For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 460 461### onAbilitySaveState<sup>12+</sup> 462 463onAbilitySaveState?(ability: UIAbility): void 464 465Called after the [onSaveState](js-apis-app-ability-uiAbility.md#onsavestate) callback of the UIAbility is triggered. 466 467**Atomic service API**: This API can be used in atomic services since API version 12. 468 469**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 470 471**Parameters** 472 473 | Name| Type| Mandatory| Description| 474 | ------ | ------ | ------ | ------ | 475 | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | Yes| UIAbility object associated with the callback event.| 476 477**Example** 478 479For details, see [AbilityLifecycleCallback Usage Example](#abilitylifecyclecallback-usage-example). 480 481### AbilityLifecycleCallback Usage Example 482 483This example demonstrates some use cases of the lifecycle listener mechanism. 4841. Register the listener when the [AbilityStage](../../application-models/abilitystage.md) is created. 4852. Unregister the listener when the [AbilityStage](../../application-models/abilitystage.md) is destroyed. 4863. Load resources when the corresponding UIAbility is created, and release resources when it is destroyed. 4874. Log events and send notifications when the UIAbility is created, destroyed, or switched between the foreground and background. 488 489```ts 490// Use AbilityLifecycleCallback to listen for the lifecycle of the UIAbility in the MyStage.ets file. 491import { AbilityLifecycleCallback, AbilityStage, application, UIAbility } from "@kit.AbilityKit"; 492import { hilog } from '@kit.PerformanceAnalysisKit'; 493import { JSON } from "@kit.ArkTS"; 494import { window } from "@kit.ArkUI"; 495import { BusinessError } from "@kit.BasicServicesKit"; 496 497const DOMAIN = 0x0000; 498const TAG = 'testTag'; 499 500function loadContent() { 501 // Load data. 502} 503 504function releaseContent() { 505 // Release data. 506} 507 508function recordAbilityEvent(abilityName: string) { 509 // Perform event tracking. 510} 511 512function publishEvent() { 513 // Publish notifications externally. 514} 515 516let abilityLifecycleCallback: AbilityLifecycleCallback = { 517 onAbilityCreate(ability: UIAbility) { 518 hilog.info(DOMAIN, TAG, 'onAbilityCreate: ' + ability.context.abilityInfo.name); 519 // Log UIAbility events. 520 recordAbilityEvent(ability.context.abilityInfo.name); 521 // Load resources and publish notifications externally when the entry UIAbility is created. 522 if (ability.context.abilityInfo.name === 'EntryAbility') { 523 loadContent(); 524 publishEvent(); 525 } 526 }, 527 onWindowStageCreate(ability: UIAbility, windowStage: window.WindowStage) { 528 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageCreate.'); 529 }, 530 onWindowStageActive(ability: UIAbility, windowStage: window.WindowStage) { 531 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageActive.'); 532 }, 533 onWindowStageInactive(ability: UIAbility, windowStage: window.WindowStage) { 534 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageInactive.'); 535 }, 536 onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage) { 537 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageDestroy.'); 538 }, 539 onAbilityDestroy(ability: UIAbility) { 540 hilog.info(DOMAIN, TAG, 'onAbilityDestroy: ' + ability.context.abilityInfo.name); 541 recordAbilityEvent(ability.context.abilityInfo.name); 542 // Release resources when the entry UIAbility is destroyed. 543 if (ability.context.abilityInfo.name === 'EntryAbility') { 544 releaseContent(); 545 publishEvent(); 546 } 547 }, 548 onAbilityForeground(ability: UIAbility) { 549 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityForeground.'); 550 recordAbilityEvent(ability.context.abilityInfo.name); 551 if (ability.context.abilityInfo.name === 'EntryAbility') { 552 publishEvent(); 553 } 554 }, 555 onAbilityBackground(ability: UIAbility) { 556 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityBackground.'); 557 recordAbilityEvent(ability.context.abilityInfo.name); 558 if (ability.context.abilityInfo.name === 'EntryAbility') { 559 publishEvent(); 560 } 561 }, 562 onAbilityContinue(ability: UIAbility) { 563 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityContinue.'); 564 }, 565 onNewWant(ability: UIAbility) { 566 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onNewWant'); 567 }, 568 onWillNewWant(ability: UIAbility) { 569 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWillNewWant'); 570 }, 571 onAbilityWillCreate(ability: UIAbility) { 572 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityWillCreate'); 573 }, 574 onWindowStageWillCreate(ability: UIAbility, windowStage: window.WindowStage) { 575 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageWillCreate'); 576 }, 577 onWindowStageWillDestroy(ability: UIAbility, windowStage: window.WindowStage) { 578 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageWillDestroy'); 579 }, 580 onAbilityWillDestroy(ability: UIAbility) { 581 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityWillDestroy'); 582 }, 583 onAbilityWillForeground(ability: UIAbility) { 584 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityWillForeground'); 585 }, 586 onAbilityWillBackground(ability: UIAbility) { 587 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityWillBackground'); 588 }, 589 onAbilityWillContinue(ability: UIAbility) { 590 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityWillContinue.'); 591 }, 592 onWindowStageWillRestore(ability: UIAbility, windowStage: window.WindowStage) { 593 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageWillRestore.'); 594 }, 595 onWindowStageRestore(ability: UIAbility, windowStage: window.WindowStage) { 596 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onWindowStageRestore.'); 597 }, 598 onAbilityWillSaveState(ability: UIAbility) { 599 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilityWillSaveState.'); 600 }, 601 onAbilitySaveState(ability: UIAbility) { 602 hilog.info(DOMAIN, TAG, 'AbilityLifecycleCallback onAbilitySaveState.'); 603 } 604}; 605 606let lifecycleId = -1; // Save the listener ID. 607 608export default class MyStage extends AbilityStage { 609 onCreate(): void { 610 hilog.info(DOMAIN, TAG, 'AbilityStage onCreate') 611 612 // Register the UIAbility lifecycle listener during AbilityStage creation and save the listener ID. 613 try { 614 let applicationContext = application.getApplicationContext(); 615 lifecycleId = applicationContext.on('abilityLifecycle', abilityLifecycleCallback); 616 } catch (e) { 617 hilog.error(DOMAIN, TAG, `register abilityLifecycle failed: ${JSON.stringify(e)}`); 618 } 619 } 620 621 onDestroy(): void { 622 // Unregister the UIAbility lifecycle listener when the AbilityStage is destroyed. 623 let applicationContext = application.getApplicationContext(); 624 applicationContext.off('abilityLifecycle', lifecycleId).catch((e: BusinessError) => { 625 hilog.error(DOMAIN, TAG, `unregister abilityLifecycle failed: ${JSON.stringify(e)}`); 626 }); 627 } 628} 629``` 630 631```ts 632// Demonstrate the entry UIAbility in EntryAbility.ets. 633import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; 634import { hilog } from '@kit.PerformanceAnalysisKit'; 635import { window } from '@kit.ArkUI'; 636 637const DOMAIN = 0x0000; 638const TAG = 'testTag'; 639 640export default class EntryAbility extends UIAbility { 641 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 642 this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); 643 hilog.info(DOMAIN, TAG, 'EntryAbility onCreate'); 644 } 645 646 onDestroy(): void { 647 hilog.info(DOMAIN, TAG, 'EntryAbility onDestroy'); 648 } 649 650 onWindowStageCreate(windowStage: window.WindowStage): void { 651 // Create the main window. 652 hilog.info(DOMAIN, TAG, 'EntryAbility onWindowStageCreate'); 653 654 windowStage.loadContent('pages/Index', (err) => { 655 if (err.code) { 656 hilog.error(DOMAIN, TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); 657 return; 658 } 659 hilog.info(DOMAIN, TAG, 'Succeeded in loading the content.'); 660 }); 661 } 662 663 onWindowStageDestroy(): void { 664 // Destroy the main window. 665 hilog.info(DOMAIN, TAG, 'EntryAbility onWindowStageDestroy'); 666 } 667 668 onForeground(): void { 669 // Switch the UIAbility to the foreground. 670 hilog.info(DOMAIN, TAG, 'EntryAbility onForeground'); 671 } 672 673 onBackground(): void { 674 // Switch the UIAbility to the background. 675 hilog.info(DOMAIN, TAG, 'EntryAbility onBackground'); 676 } 677} 678``` 679