1、定义一个生命周期类ExitAppLifecycle实现IHMLifecycle接口
import { HMLifecycle, HMLifecycleContext, IHMLifecycle } from '@hadss/hmrouter'; @HMLifecycle({lifecycleName: 'ExitAppLifecycle'}) export class ExitAppLifecycle implements IHMLifecycle { private lastTime: number = 0; // 上一次后退操作时间 // 后退按钮事件 onBackPressed(ctx: HMLifecycleContext): boolean { let time = new Date().getTime(); if(time - this.lastTime > 2000) { this.lastTime = time; ctx.uiContext.getPromptAction().showToast({ message: '再按一次返回键回到桌面', duration: 2000 }); return true; } else { return false; } } }
2、相关页面关联该生命周期类
// 绑定生命周期及自定义转场动画 @HMRouter({ pageUrl: 'Main', singleton:true,lifecycle: 'ExitAppLifecycle', animator: 'pageAnimator' }) @Component // 注意HMRouter页面需要export export struct Main { builder{} }标签:实战,IHMLifecycle,鸿蒙,HMRouter,ExitAppLifecycle,export,time,lastTime From: https://www.cnblogs.com/xqxacm/p/18560932