@ohos.app.ability.UIAbility (UIAbility)
一、说明
UIAbility是包含UI界面的应用组件,提供组件创建、销毁、前后台切换等生命周期回调,同时也具备组件协同的能力,组件协同主要提供如下常用功能。
Caller,由startAbilityByCall接口返回,CallerAbility(调用者)可使用Caller与CalleeAbility(被调用者)进行通信。
Callee,UIAbility的内部对象,CalleeAbility(被调用者)可以通过Callee与Caller进行通信。本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。本模块接口仅可在Stage模型下使用。
二、导入模块
import Ability from ‘@ohos.app.ability.UIAbility’;
三、属性
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.AbilityCore
1.UIAbility.onCreate
onCreate(want: Want, param: AbilityConstant.LaunchParam): void;
Ability创建时回调,执行初始化业务逻辑操作。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
参数:
示例:
class myAbility extends Ability {
onCreate(want, param) {
console.log('onCreate, want:' + want.abilityName);
}
}
2.UIAbility.onWindowStageCreate
onWindowStageCreate(windowStage: window.WindowStage): void
当WindowStage创建后调用。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
参数:
示例:
class myAbility extends Ability {
onWindowStageCreate(windowStage) {
console.log('onWindowStageCreate');
}
}
3.UIAbility.onWindowStageDestroy
onWindowStageDestroy(): void
当WindowStage销毁后调用。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
class myAbility extends Ability {
onWindowStageDestroy() {
console.log('onWindowStageDestroy');
}
}
4.UIAbility.onWindowStageRestore
onWindowStageRestore(windowStage: window.WindowStage): void
当迁移多实例ability时,恢复WindowStage后调用。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
参数:
示例:
class myAbility extends Ability {
onWindowStageRestore(windowStage) {
console.log('onWindowStageRestore');
}
}
5.UIAbility.onDestroy
onDestroy(): void | Promise<void>;
Ability生命周期回调,在销毁时回调,执行资源清理等操作。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
class myAbility extends Ability {
onDestroy() {
console.log('onDestroy');
}
}
6.UIAbility.onForeground
onForeground(): void;
Ability生命周期回调,当应用从后台转到前台时触发。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
class myAbility extends Ability {
onForeground() {
console.log('onForeground');
}
}
7.UIAbility.onBackground
onBackground(): void;
Ability生命周期回调,当应用从前台转到后台时触发。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
class myAbility extends Ability {
onBackground() {
console.log('onBackground');
}
}
8.UIAbility.onContinue
onContinue(wantParam: { [key: string]: Object }): AbilityConstant.OnContinueResult;
当ability迁移准备迁移时触发,保存数据。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
import AbilityConstant from ‘@ohos.app.ability.AbilityConstant’;
class MyUIAbility extends Ability {
onContinue(wantParams) {
console.log('onContinue');
wantParams['myData'] = 'my1234567';
return AbilityConstant.OnContinueResult.AGREE;
}
}
9.UIAbility.onNewWant
onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void;
当传入新的Want,ability再次被拉起时会回调执行该方法。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
class MyUIAbility extends Ability {
onNewWant(want, launchParams) {
console.log('onNewWant, want:' + want.abilityName);
console.log('onNewWant, launchParams:' + JSON.stringify(launchParams));
}
}
10.UIAbility.onDump
onDump(params: Array<string>): Array<string>;
转储客户端信息时调用。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
class myAbility extends Ability {
onDump(params) {
console.log('dump, params:' + JSON.stringify(params));
return ['params'];
}
}
11.UIAbility.onSaveState
onSaveState(reason: AbilityConstant.StateType, wantParam : {[key: string]: Object}): AbilityConstant.OnSaveResult;
该API配合appRecovery使用。在应用故障时,如果使能了自动保存状态,框架将回调onSaveState保存Ability状态。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
import AbilityConstant from ‘@ohos.app.ability.AbilityConstant’;
class MyUIAbility extends Ability {
onSaveState(reason, wantParam) {
console.log('onSaveState');
wantParam['myData'] = 'my1234567';
return AbilityConstant.OnSaveResult.RECOVERY_AGREE;
}
}
本文根据HarmonyOS官方开发文档整理
标签:原生,console,Ability,鸿蒙,SystemCapability,UIAbility,AbilityRuntime,log,Stage From: https://blog.51cto.com/u_14946066/8924484