鸿蒙项目实战(六):HMRouter实现两次返回退出应用
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{} }
作者:听着music睡
出处:http://www.cnblogs.com/xqxacm/
Android交流群:38197636
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步