提交 e59c72cb 编写于 作者: D donglin

add onPrepareToTerminate interface

Signed-off-by: Ndonglin <donglin9@huawei.com>
Change-Id: I0b0981b90e9bc5ca15208734ce995b472b82e971
上级 575137d2
......@@ -339,6 +339,51 @@ class MyUIAbility extends UIAbility {
}
```
## UIAbility.onPrepareToTerminate<sup>10+</sup>
onPrepareToTerminate(): boolean;
UIAbility生命周期回调,当系统预关闭开关打开后(配置系统参数persist.sys.prepare_terminate为true打开),在UIAbility关闭时触发,可在回调中定义操作来决定是否继续执行关闭UIAbility的操作。如果UIAbility在退出时需要与用户交互确认是否关闭UIAbility,可在此生命周期回调中定义预关闭操作配合[terminateSelf](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself)接口退出,如弹窗确认是否关闭,并配置预关闭生命周期返回true取消正常关闭。
**需要权限**:ohos.permission.PREPARE_APP_TERMINATE
**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
**返回值:**
| 类型 | 说明 |
| -- | -- |
| boolean | 是否执行UIAbility关闭操作,返回true表示本次UIAbility关闭流程取消,不再退出,返回false表示UIAbility继续正常关闭。 |
**示例:**
```ts
export default class EntryAbility extends UIAbility {
onPrepareToTermiante() {
// 开发者定义预关闭动作
// 例如拉起另一个ability,根据ability处理结果执行异步关闭
let want:Want = {
bundleName: "com.example.myapplication",
moduleName: "entry",
abilityName: "SecondAbility"
}
this.context.startAbilityForResult(want)
.then((result)=>{
// 获取ability处理结果,当返回结果的resultCode为0关闭当前UIAbility
console.log('startAbilityForResult success, resultCode is ' + result.resultCode);
if (result.resultCode === 0) {
this.context.terminateSelf();
}
}).catch((err)=>{
// 异常处理
console.log('startAbilityForResult failed, err:' + JSON.stringify(err));
this.context.terminateSelf();
})
return true; // 已定义预关闭操作后,返回true表示UIAbility取消关闭
}
}
```
## Caller
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册