生命周期发生变化时触发相应回调

2024-01-22 18:53 更新

AbilityLifecycleCallback模块提供应用上下文ApplicationContext的生命周期发生变化时触发相应回调的能力,包括onAbilityCreateonWindowStageCreateonWindowStageActiveonWindowStageInactiveonWindowStageDestroyonAbilityDestroyonAbilityForegroundonAbilityBackgroundonAbilityContinue方法。

说明

本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅可在Stage模型下使用。

导入模块

  1. import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

AbilityLifecycleCallback.onAbilityCreate

onAbilityCreate(ability: UIAbility): void;

注册监听应用上下文的生命周期后,在ability创建时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象

示例:

  1. let abilityLifecycleCallback = {
  2. onAbilityCreate(ability){
  3. console.log('AbilityLifecycleCallback onAbilityCreate.');
  4. }
  5. };

AbilityLifecycleCallback.onWindowStageCreate

onWindowStageCreate(ability: UIAbility, windowStage: window.WindowStage): void;

注册监听应用上下文的生命周期后,在windowStage创建时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象
windowStagewindow.WindowStage当前WindowStage对象

示例:

  1. let abilityLifecycleCallback = {
  2. onWindowStageCreate(ability, windowStage){
  3. console.log('AbilityLifecycleCallback onWindowStageCreate.');
  4. }
  5. };

AbilityLifecycleCallback.onWindowStageActive

onWindowStageActive(ability: UIAbility, windowStage: window.WindowStage): void;

注册监听应用上下文的生命周期后,在windowStage获焦时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象
windowStagewindow.WindowStage当前WindowStage对象

示例:

  1. let abilityLifecycleCallback = {
  2. onWindowStageActive(ability, windowStage){
  3. console.log('AbilityLifecycleCallback onWindowStageActive.');
  4. }
  5. };

AbilityLifecycleCallback.onWindowStageInactive

onWindowStageInactive(ability: UIAbility, windowStage: window.WindowStage): void;

注册监听应用上下文的生命周期后,在windowStage失焦时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象
windowStagewindow.WindowStage当前WindowStage对象

示例:

  1. let abilityLifecycleCallback = {
  2. onWindowStageInactive(ability, windowStage){
  3. console.log('AbilityLifecycleCallback onWindowStageInactive.');
  4. }
  5. };

AbilityLifecycleCallback.onWindowStageDestroy

onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void;

注册监听应用上下文的生命周期后,在windowStage销毁时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象
windowStagewindow.WindowStage当前WindowStage对象

示例:

  1. let abilityLifecycleCallback = {
  2. onWindowStageDestroy(ability, windowStage){
  3. console.log('AbilityLifecycleCallback onWindowStageDestroy.');
  4. }
  5. };

AbilityLifecycleCallback.onAbilityDestroy

onAbilityDestroy(ability: UIAbility): void;

注册监听应用上下文的生命周期后,在ability销毁时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象

示例:

  1. let abilityLifecycleCallback = {
  2. onAbilityDestroy(ability){
  3. console.log('AbilityLifecycleCallback onAbilityDestroy.');
  4. }
  5. };

AbilityLifecycleCallback.onAbilityForeground

onAbilityForeground(ability: UIAbility): void;

注册监听应用上下文的生命周期后,在ability的状态从后台转到前台时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象

示例:

  1. let abilityLifecycleCallback = {
  2. onAbilityForeground(ability){
  3. console.log('AbilityLifecycleCallback onAbilityForeground.');
  4. }
  5. };

AbilityLifecycleCallback.onAbilityBackground

onAbilityBackground(ability: UIAbility): void;

注册监听应用上下文的生命周期后,在ability的状态从前台转到后台时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象

示例:

  1. let abilityLifecycleCallback = {
  2. onAbilityBackground(ability){
  3. console.log('AbilityLifecycleCallback onAbilityBackground.');
  4. }
  5. };

AbilityLifecycleCallback.onAbilityContinue

onAbilityContinue(ability: UIAbility): void;

注册监听应用上下文的生命周期后,在ability迁移时触发回调。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

参数:

参数名类型必填说明
abilityUIAbility当前Ability对象

示例:

  1. let abilityLifecycleCallback = {
  2. onAbilityContinue(ability){
  3. console.log('AbilityLifecycleCallback onAbilityContinue.');
  4. }
  5. };

AbilityLifecycleCallback使用

示例:

MyFirstAbility.ts

  1. import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';
  2. import AbilityStage from '@ohos.app.ability.AbilityStage';
  3. import UIAbility from '@ohos.app.ability.UIAbility';
  4. // 声明ability生命周期回调,需配置所有回调后才可以在applicationContext注册
  5. let abilityLifecycleCallback = {
  6. onAbilityCreate(ability){
  7. console.log('AbilityLifecycleCallback onAbilityCreate.');
  8. },
  9. onWindowStageCreate(ability, windowStage){
  10. console.log('AbilityLifecycleCallback onWindowStageCreate.');
  11. },
  12. onWindowStageActive(ability, windowStage){
  13. console.log('AbilityLifecycleCallback onWindowStageActive.');
  14. },
  15. onWindowStageInactive(ability, windowStage){
  16. console.log('AbilityLifecycleCallback onWindowStageInactive.');
  17. },
  18. onWindowStageDestroy(ability, windowStage){
  19. console.log('AbilityLifecycleCallback onWindowStageDestroy.');
  20. },
  21. onAbilityDestroy(ability){
  22. console.log('AbilityLifecycleCallback onAbilityDestroy.');
  23. },
  24. onAbilityForeground(ability){
  25. console.log('AbilityLifecycleCallback onAbilityForeground.');
  26. },
  27. onAbilityBackground(ability){
  28. console.log('AbilityLifecycleCallback onAbilityBackground.');
  29. },
  30. onAbilityContinue(ability){
  31. console.log('AbilityLifecycleCallback onAbilityContinue.');
  32. }
  33. };
  34. export default class MyFirstAbility extends UIAbility {
  35. onCreate() {
  36. console.log('MyAbilityStage onCreate');
  37. // 1.通过context属性获取applicationContext
  38. let applicationContext = this.context.getApplicationContext();
  39. // 2.通过applicationContext注册监听应用内生命周期
  40. try {
  41. globalThis.lifecycleId = applicationContext.on('abilityLifecycle', abilityLifecycleCallback);
  42. console.log('registerAbilityLifecycleCallback lifecycleId: ${globalThis.lifecycleId}');
  43. } catch (paramError) {
  44. console.log('error: ' + paramError.code + ' ,' + paramError.message);
  45. }
  46. }
  47. }

MySecondAbility.ts

  1. import UIAbility from '@ohos.app.ability.UIAbility';
  2. export default class MySecondAbility extends UIAbility {
  3. onDestroy() {
  4. let applicationContext = this.context.getApplicationContext();
  5. // 3.通过applicationContext注销监听应用内生命周期
  6. applicationContext.off('abilityLifecycle', globalThis.lifecycleId, (error) => {
  7. if (error.code != 0) {
  8. console.log('unregisterAbilityLifecycleCallback failed, error: ' + JSON.stringify(error));
  9. } else {
  10. console.log('unregisterAbilityLifecycleCallback success.');
  11. }
  12. });
  13. }
  14. }
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号