import { UIAbility } from '@kit.AbilityKit';
export default class MyAbility extends UIAbility {
onBackground() {
let applicationContext = this.context.getApplicationContext();
applicationContext.killAllProcesses();
}
} @Entry
@Component
struct IndexComponent {
@State textColor: Color = Color.Black;
onBackPress() {
new process.ProcessManager().exit(0);
return true;
}
build() {
Column() {
Text('Hello World')
.fontColor(this.textColor)
.fontSize(30)
.margin(30)
}.width('100%')
}
} 修改应用退出方式,建议使用terminateSelf()这种退出方式,它用于停止当前Ability自身,提供了动画过渡,用户体验较好。
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct IndexComponent {
@State textColor: Color = Color.Black;
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
onPageShow() {
this.textColor = Color.Blue;
console.info('IndexComponent onPageShow');
}
onPageHide() {
this.textColor = Color.Transparent;
console.info('IndexComponent onPageHide');
}
onBackPress() {
this.textColor = Color.Red;
console.info('IndexComponent onBackPress');
this.context.terminateSelf((err: BusinessError) => {
if (err.code) {
// 处理业务逻辑错误
console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
return;
}
// 执行正常业务
console.info('terminateSelf succeed');
});
}
build() {
Column() {
Text('Hello World')
.fontColor(this.textColor)
.fontSize(30)
.margin(30)
}.width('100%')
}
}