网站建设教程,国外手机网站设计,网络舆情监测与预警系统通过对海量,企业官网建站费用前言#xff1a;在使用ArkTs语言写鸿蒙的App中#xff0c;我们发现Page的生命周期函数#xff0c;如下#xff1a;
页面的生命周期(32)
onPageShow:页面显示触发(页面特有)
onPageHide#xff1a;页面隐藏触发(页面特有)
onBackPress#xff1a;当用户点击返回按钮时…前言在使用ArkTs语言写鸿蒙的App中我们发现Page的生命周期函数如下
页面的生命周期(32)
onPageShow:页面显示触发(页面特有)
onPageHide页面隐藏触发(页面特有)
onBackPress当用户点击返回按钮时触发(页面特有)
aboutToAppear组件即将出现时触发
aboutToDisappear组件即将析构销毁时触发
组件的生命周期(2)
aboutToAppear组件即将出现时触发
aboutToDisappear组件即将析构销毁时触发
UIAbility组件生命周期函数
onCreate: UIAbility实例创建完成时触发
onForeground: 在UIAbility的UI可见之前
onBackground: 在UIAbility的UI完全不可见之后
onDestroy: 在UIAbility实例销毁时触发
WindowStage窗口生命周期函数
onWindowStageCreate 窗口才能构建
onWindowStageDestroy 窗口销毁
而我们的页面生命周期函数aboutToAppear只会触发一次如果我们的页面视图需要每次出现的时候都要刷新或者请求就会发现没法触发生命周期函数在ios的项目中有viewwillappare这个函数这样就很好处理但是鸿蒙中没有这样的生命周期函数。 我们的项目路由采用的是hmrouter框架hmrouter的框架中的生命周期方法就比鸿蒙系统的方法就多了很多。hmrouter的具体用法我就不在此详细介绍了需要学习的可以去官网学习下用法。生命周期如下
export abstract class AbstractLifecycle implements IHMLifecycle {observerMap: MapHMLifecycleState, Array(ctx: HMLifecycleContext) HMLifecycleAction new Map()onPrepare(ctx: HMLifecycleContext): HMLifecycleAction {return HMLifecycleAction.DO_NEXT()}onAppear(ctx: HMLifecycleContext): HMLifecycleAction {return HMLifecycleAction.DO_NEXT()}onDisAppear(ctx: HMLifecycleContext): HMLifecycleAction {return this.runObserver(HMLifecycleState.onDisAppear, ctx)}onShown(ctx: HMLifecycleContext): HMLifecycleAction {return this.runObserver(HMLifecycleState.onShown, ctx)}onHidden(ctx: HMLifecycleContext): HMLifecycleAction {return this.runObserver(HMLifecycleState.onHidden, ctx)}onWillAppear(ctx: HMLifecycleContext): HMLifecycleAction {return HMLifecycleAction.DO_NEXT()}onWillDisappear(ctx: HMLifecycleContext): HMLifecycleAction {return this.runObserver(HMLifecycleState.onWillDisappear, ctx)}onWillShow(ctx: HMLifecycleContext): HMLifecycleAction {return this.runObserver(HMLifecycleState.onWillShow, ctx)}onWillHide(ctx: HMLifecycleContext): HMLifecycleAction {return this.runObserver(HMLifecycleState.onWillHide, ctx)}onReady(ctx: HMLifecycleContext): HMLifecycleAction {return HMLifecycleAction.DO_NEXT()}onBackPressed(ctx: HMLifecycleContext): boolean {return this.runObserver(HMLifecycleState.onBackPressed, ctx).value}hmrouter的生命周期监听方法太多了完全够用了。具体用法如下
export class PageDurationLifecycle extends AbstractLifecycle {private timeMap: Mapstring, number new Map();onShown(ctx: HMLifecycleContext): HMLifecycleAction {const pageName ctx.navContext?.pathInfo.name;let param ctx.navContext?.pathInfo.param as object;WinLog.info(PageDurationLifecycle onShown pageName pageName)if (pageName) {this.timeMap.set(pageName, new Date().getTime())if (pageNameLoginPage){AppStorage.setboolean(loginOnPageShow, true);}}return HMLifecycleAction.DO_NEXT();}onHidden(ctx: HMLifecycleContext): HMLifecycleAction {const pageName ctx.navContext?.pathInfo.nameWinLog.info(PageDurationLifecycle onHidden pageName pageName)if (pageName this.timeMap.has(pageName)) {const duration new Date().getTime() - (this.timeMap.get(pageName) as number);this.timeMap.delete(pageName);WinLog.info(Page ${pageName} stay ${duration} ms);if (pageNameLoginPage){AppStorage.setboolean(loginOnPageShow, false);}}return HMLifecycleAction.DO_NEXT();}}你通过日志观察就会发现每当LoginPage出现的时候就会调用onShown方法消失的时候就会调用onHidden方法那我采用的是AppStorage存储页面的状态通过监听loginOnPageShow的值来判断页面的生命周期
在LoginPage页面
StorageProp(loginOnPageShow) Watch(onChangeVisibility) pageVisible: boolean false;onChangeVisibility(){WinLog.debug(MVListAcvtPage 是否展示---this.curPageVisibility)if (this.curPageVisibility) {this.getSubFuncBeanVisitAction()}}
hmrouter
这样就通过hmrouter框架实现了Page的生命周期的监听当然hmrouter框架还有很多生命周期方法你都可以试试。