一、说明 AbilityLifecycleCallback模块提供应用上下文ApplicationContext的生命周期发生变化时触发相应回调的能力,包括onAbilityCreate、onWindowStageCreate、onWindowStageActive、onWindowStageInactive、onWindowStageDestroy、onAbilityDestroy、onAbilityForeground、onAbilityBackground、onAbilityContinue方法。本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。本模块接口仅可在Stage模型下使用。 二、导入模块 import AbilityLifecycleCallback from ‘@ohos.app.ability.AbilityLifecycleCallback’; 1.AbilityLifecycleCallback.onAbilityCreate onAbilityCreate(ability: UIAbility): void; 注册监听应用上下文的生命周期后,在ability创建时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第1张图片](http://img.e-com-net.com/image/info8/d60635b2ebd04008b18e43dfeed0907d.jpg) 示例: let abilityLifecycleCallback = { onAbilityCreate(ability){ console.log(‘AbilityLifecycleCallback onAbilityCreate.’); } }; 2.AbilityLifecycleCallback.onWindowStageCreate onWindowStageCreate(ability: UIAbility, windowStage: window.WindowStage): void; 注册监听应用上下文的生命周期后,在windowStage创建时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第2张图片](http://img.e-com-net.com/image/info8/02b91da06c2c42e1a785b29ade92285e.jpg) 示例: let abilityLifecycleCallback = { onAbilityCreate(ability){ console.log(‘AbilityLifecycleCallback onAbilityCreate.’); } }; 3.AbilityLifecycleCallback.onWindowStageCreate onWindowStageCreate(ability: UIAbility, windowStage: window.WindowStage): void; 注册监听应用上下文的生命周期后,在windowStage创建时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第3张图片](http://img.e-com-net.com/image/info8/604bf3414ec24b0caf6b9edb801ea890.jpg) 示例: let abilityLifecycleCallback = { onWindowStageActive(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageActive.’); } }; 4.AbilityLifecycleCallback.onWindowStageInactive onWindowStageInactive(ability: UIAbility, windowStage: window.WindowStage): void; 注册监听应用上下文的生命周期后,在windowStage失焦时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第4张图片](http://img.e-com-net.com/image/info8/6198293116234203b494d2d6ed77bade.jpg) 示例: let abilityLifecycleCallback = { onWindowStageInactive(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageInactive.’); } }; 5.AbilityLifecycleCallback.onWindowStageDestroy onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void; 注册监听应用上下文的生命周期后,在windowStage销毁时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第5张图片](http://img.e-com-net.com/image/info8/c690b8fe642649d6be6fdc17e7c3fa5c.jpg) 示例: let abilityLifecycleCallback = { onWindowStageInactive(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageInactive.’); } }; 6.AbilityLifecycleCallback.onWindowStageDestroy onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void; 注册监听应用上下文的生命周期后,在windowStage销毁时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第6张图片](http://img.e-com-net.com/image/info8/066782ccbe154d059dea0413ce0753dc.jpg) 示例: let abilityLifecycleCallback = { onWindowStageInactive(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageInactive.’); } }; 7.AbilityLifecycleCallback.onWindowStageDestroy onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void; 注册监听应用上下文的生命周期后,在windowStage销毁时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第7张图片](http://img.e-com-net.com/image/info8/23804fbfb59e4ed789cd8f05891d1f68.jpg) 示例: let abilityLifecycleCallback = { onWindowStageInactive(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageInactive.’); } }; 8.AbilityLifecycleCallback.onWindowStageDestroy onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void; 注册监听应用上下文的生命周期后,在windowStage销毁时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第8张图片](http://img.e-com-net.com/image/info8/aac75b8f4f4844d0b493e03f59e2e9ce.jpg) 示例: let abilityLifecycleCallback = { onAbilityBackground(ability){ console.log(‘AbilityLifecycleCallback onAbilityBackground.’); } }; 9.AbilityLifecycleCallback.onAbilityContinue onAbilityContinue(ability: UIAbility): void; 注册监听应用上下文的生命周期后,在ability迁移时触发回调。 系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore 参数: ![鸿蒙原生应用/元服务开发-Stage模型能力接口(三)_第9张图片](http://img.e-com-net.com/image/info8/68f45985e1744239a82407c6c2f1b7fb.jpg) 示例: let abilityLifecycleCallback = { onAbilityContinue(ability){ console.log(‘AbilityLifecycleCallback onAbilityContinue.’); } }; AbilityLifecycleCallback使用 示例: MyFirstAbility.ts import AbilityLifecycleCallback from ‘@ohos.app.ability.AbilityLifecycleCallback’; import AbilityStage from ‘@ohos.app.ability.AbilityStage’; import UIAbility from ‘@ohos.app.ability.UIAbility’; // 声明ability生命周期回调,需配置所有回调后才可以在applicationContext注册 let abilityLifecycleCallback = { onAbilityCreate(ability){ console.log(‘AbilityLifecycleCallback onAbilityCreate.’); }, onWindowStageCreate(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageCreate.’); }, onWindowStageActive(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageActive.’); }, onWindowStageInactive(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageInactive.’); }, onWindowStageDestroy(ability, windowStage){ console.log(‘AbilityLifecycleCallback onWindowStageDestroy.’); }, onAbilityDestroy(ability){ console.log(‘AbilityLifecycleCallback onAbilityDestroy.’); }, onAbilityForeground(ability){ console.log(‘AbilityLifecycleCallback onAbilityForeground.’); }, onAbilityBackground(ability){ console.log(‘AbilityLifecycleCallback onAbilityBackground.’); }, onAbilityContinue(ability){ console.log(‘AbilityLifecycleCallback onAbilityContinue.’); } }; export default class MyFirstAbility extends UIAbility { onCreate() { console.log(‘MyAbilityStage onCreate’); // 1.通过context属性获取applicationContext let applicationContext = this.context.getApplicationContext(); // 2.通过applicationContext注册监听应用内生命周期 try { globalThis.lifecycleId = applicationContext.on(‘abilityLifecycle’, abilityLifecycleCallback); console.log(‘registerAbilityLifecycleCallback lifecycleId: ${globalThis.lifecycleId}’); } catch (paramError) { console.log(‘error: ’ + paramError.code + ’ ,’ + paramError.message); } } } MySecondAbility.ts import UIAbility from ‘@ohos.app.ability.UIAbility’; export default class MySecondAbility extends UIAbility { onDestroy() { let applicationContext = this.context.getApplicationContext(); // 3.通过applicationContext注销监听应用内生命周期 applicationContext.off(‘abilityLifecycle’, globalThis.lifecycleId, (error) => { if (error.code != 0) { console.log('unregisterAbilityLifecycleCallback failed, error: ’ + JSON.stringify(error)); } else { console.log(‘unregisterAbilityLifecycleCallback success.’); } }); } } 本文根据HarmonyOS官方开发文档整理 |