赞
踩
在编辑场景中,存在用户误触返回,导致内容未保存就退出编辑页的现象; 本示例介绍使用NavDestination组件的onBackPressed回调对返回事件进行拦截,提示用户保存编辑内容,并使用preferences实例持久化保存内容。
使用说明
因为功能是以har的形式集成在主工程中,没有@Entry修饰的组件,也无法作为入口组件,不能使用@Entry组件的onBackPress生命周期函数。
在使用方面onBackPressed是NavDestination自己的事件,需配合NavDestination组件使用; 且组件本身用于显示Navigation内容区,作为子页面的根容器,因此若拦截子页面的返回事件,可使用onBackPressed回调。
onBackPress生命周期函数只能在@Entry组件中使用,因此若对入口组件的返回事件拦截可使用onBackPress生命周期函数。
通过NavDestination组件的onBackPressed回调对返回事件进行拦截;源码参考MainPage.ets。
// 应用主页用NavDestination承载,用于显示Navigation的内容区 Navigation(this.pageStackForComponentSharedPages) { } .onAppear(() => { this.pageStackForComponentSharedPages.pushPathByName("MainPage", null, false); }) // 创建NavDestination组件,需使用此组件的onBackPressed回调拦截返回事件 .navDestination(this.textArea) @Builder textArea(name: string) { NavDestination() { Column() { TextArea({ text: this.text, placeholder: 'input your word...', controller: this.controller }) .onChange((value: string) => { this.text = value; }) } .justifyContent(FlexAlign.Start) .width('100%') .height('100%') } .onBackPressed(() => { // 此处可添加拦截处理逻辑,然后return true放行 return true } }
不涉及
sideslipintercept // har类型
|---mainpage
| |---Mainpage.ets // 视图层-组件页面
本场景依赖了路由模块来注册路由。
@ohos.data.preferences (用户首选项)
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr18.cn/F781PH
https://qr18.cn/F781PH
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。