【HarmonyOS】HMRouter使用详解(三)生命周期

devtools/2024/10/15 14:35:02/

生命周期(Lifecycle)


使用HMRouter的页面跳转时,想实现和Navigation一样的生命周期时,需要通过新建生命周期类来实现对页面对某一个生命周期的监控。

新建Lifecycle类


通过继承IHMLifecycle接口实现生命周期接口的方法重写。
通过添加@HMLifecycle装饰器,来定义生命周期类的名称,然后在页面中使用

IHMLifecycle

export interface IHMLifecycle {onPrepare?(ctx: HMLifecycleContext): void;onAppear?(ctx: HMLifecycleContext): void;onDisAppear?(ctx: HMLifecycleContext): void;onShown?(ctx: HMLifecycleContext): void;onHidden?(ctx: HMLifecycleContext): void;onWillAppear?(ctx: HMLifecycleContext): void;onWillDisappear?(ctx: HMLifecycleContext): void;onWillShow?(ctx: HMLifecycleContext): void;onWillHide?(ctx: HMLifecycleContext): void;onReady?(ctx: HMLifecycleContext): void;onBackPressed?(ctx: HMLifecycleContext): boolean;
}
  • onPrepare:在拦截器执行后,路由栈真正push前触发。
  • onWillAppear:在路由组件创建后,挂载到组件树之前执行。
  • onAppear:通用生命周期事件,路由组件挂载到组件树时执行。
  • onWillShow:路由组件布局显示之前执行,此时页面不可见(应用切换到前台不会触发)。
  • onShown:路由组件布局显示之后执行,此时页面已完成布局。
  • onWillHide:路由组件触发隐藏之前执行(应用切换到后台不会触发)。
  • onHidden:路由组件触发隐藏后执行(非栈顶页面push进栈,栈顶页面pop出栈或应用切换到后台)。
  • onWillDisappear:路由组件即将销毁之前执行,如果有转场动画,会在动画前触发(栈顶页面pop出栈)。
  • onDisappear:通用生命周期事件,路由组件从组件树上卸载销毁时执行。
  • onReady:在即将构件子组件时触发此回调。
  • onBackPressed:在路由组件绑定的页面栈中存在内容时,此回调生效。当点击返回键时,触发该回调。返回值为true时,表示重写返回键逻辑,false时,表示回退到上一个页面。

下面插入Navigation的生命周期流程图,HMRouter的生命周期流程类似,在此基础上增加了额外的生命周期流程。

@HMLifecycle装饰器

export declare function HMLifecycle(param: HMLifecycleParam): ObjectConstructor;
export interface HMLifecycleParam {lifecycleName: string;priority?: number;global?: boolean;
}

标记在实现了IHMLifecycle的对象上,声明此对象为一个自定义生命周期处理器。

  • lifecycleName:自定义生命周期处理器名称,必填。
  • priority:生命周期优先等级。按照优先等级顺序触发,不区分自定义或者全局生命周期,优先级相同时先执行@HMRouter中定义的自定义生命周期。
  • global:是否为全局生命周期,true时,所有页面生命周期事件为当前设定的生命周期处理器,默认为false。

实现代码


在之前文章的基础上进行修改。
添加一个Lifecycles文件夹,并新建一个TwoPageLifecycle,来实现TwoPage页面的生命周期。

TwoPageLifecycle

import { HMLifecycle, HMLifecycleContext, IHMLifecycle } from "@hadss/hmrouter";@HMLifecycle({ lifecycleName: 'TwoPageLifecycle' })
export class TwoPageLifecycle implements IHMLifecycle {/*** 在拦截器执行后,路由栈真正push前触发* @param ctx*/onPrepare(ctx: HMLifecycleContext): void {console.debug("router", 'onPrepare');}onWillAppear(ctx: HMLifecycleContext): void {console.debug("router", 'onWillAppear');}onAppear(ctx: HMLifecycleContext): void {console.debug("router", 'onAppear');}onWillShow(ctx: HMLifecycleContext): void {console.debug("router", 'onWillShow');}onShown(ctx: HMLifecycleContext): void {console.debug("router", 'onShown');}onWillHide(ctx: HMLifecycleContext): void {console.debug("router", 'onWillHide');}onHidden(ctx: HMLifecycleContext): void {console.debug("router", 'onHidden');}onWillDisappear(ctx: HMLifecycleContext): void {console.debug("router", 'onWillDisappear');}onDisAppear(ctx: HMLifecycleContext): void {console.debug("router", 'onDisAppear');}onReady(ctx: HMLifecycleContext): void {console.debug("router", 'onReady');}onBackPressed(ctx: HMLifecycleContext): boolean {console.debug("router", 'onBackPressed');return true;}
}

TwoPage

import { HMPopInfo, HMRouter, HMRouterMgr } from '@hadss/hmrouter'
import { PageModel } from '../../Models/PageModel'@HMRouter({ pageUrl: "TwoPage", lifecycle: "TwoPageLifecycle" })
@Component
export struct TwoPage {aboutToAppear(): void {let currentParam: PageModel = HMRouterMgr.getCurrentParam() as PageModel;if (currentParam == undefined) {return;}console.debug("router", 'name:' + currentParam.Name);console.debug("router", 'age:' + currentParam.Age);}build() {Column({ space: 20 }) {Button("ThreePage").width("80%").onClick(() => {HMRouterMgr.push({navigationId: "mainNavigation",pageUrl: "ThreePage"}, {onResult: (popInfo: HMPopInfo) => {let popResult: PageModel = popInfo.result as PageModel;if (popResult == null || popResult == undefined) {return;}console.debug("router", 'name:' + popResult.Name);console.debug("router", 'age:' + popResult.Age);}})})Button("ThreeReplacePage").width("80%").onClick(() => {HMRouterMgr.replace({navigationId: "mainNavigation",pageUrl: "ThreePage"}, {onResult: (popInfo: HMPopInfo) => {let popResult: PageModel = popInfo.result as PageModel;if (popResult == null || popResult == undefined) {return;}console.debug("router", 'name:' + popResult.Name);console.debug("router", 'age:' + popResult.Age);}})})Button("HomePage").width("80%").onClick(() => {HMRouterMgr.pop({navigationId: "mainNavigation"})})}.height("100%").width("100%")}
}

实现效果

在生命周期方法中实现内容打印,截图如下:

可以看到生命周期的调用顺序


http://www.ppmy.cn/devtools/126206.html

相关文章

《软件工程概论》作业一:新冠疫情下软件产品设计

课程说明:《软件工程概论》为浙江科技学院2018级软件工程专业在大二下学期开设的必修课。课程使用《软件工程导论(第6版)》(张海藩等编著,清华大学出版社)作为教材。以《软件设计文档国家标准GBT8567-2006》…

spring-第三章 spring入门程序

spring 文章目录 spring前言1.依赖引入1.1依赖包介绍1.2基础依赖引入 2.IOC功能使用2.1xml文件2.1.1bean**标签介绍**2.1.2配置实例 2.2使用bean2.2.1获取IOC容器2.2.2获取bean对象 3.补充说明4.开启log4j2日志4.1引入依赖4.2配置文件4.3使用日志 总结 前言 这篇文章中我们简单…

【算法——递归回溯】

这个东西还是很重要的&#xff0c;直接决定了你的动态规划章节的学习深度 78. 子集 方法1&#xff1a; vector<vector<int>>V; void dfs(vector<int> v,vector<int> nums,int index) {if(indexnums.size()) V.push_back(v);else{v.push_back(nums[i…

【获取简易网页存储的密码】

拜访客户&#xff0c;电脑端连上客户的访客网络后&#xff0c;发现手机还没连&#xff0c;同时网页上的密码被隐藏显示了&#xff0c;自己也忘了访客密码是多少了。这种安全性要求不高的网页隐藏密码如何查看&#xff1a; 1.如下&#xff0c;电脑连上了网络&#xff0c;但自己…

Linux 常用命令 - file 【识别文件类型】

简介 file 命令源自英语单词 “file”&#xff0c;直译为“文件”。在 Linux 系统中&#xff0c;file 命令用于确定文件类型。它通过检查文件的内容和某些情况下的文件头信息&#xff0c;来判断文件的具体类型&#xff08;如文本、二进制、执行文件等&#xff09;。 使用方式…

Pyspark中pyspark.sql.functions常用方法(1)

文章目录 pyspark sql functions&#xff08;1&#xff09;spark.rangecol alias columnlit 创建常量列broadcast 广播表coalesce 合并列 (none)isnan 判断nan值isnull 判断None值nanvl 合并列 &#xff08;nan&#xff09;udf 自定义函数rand 随机列 &#xff0c;randn 随机正…

嵌入式Linux开发板配置静态IP

嵌入式Linux开发板配置静态IP Chapter1 嵌入式Linux开发板配置静态IPChapter2 Linux命令之hwclock - 查询和设置硬件时钟 Chapter1 嵌入式Linux开发板配置静态IP 修改interfaces配置文件&#xff0c;普通用户interfaces文件权限只可读&#xff0c;首先切换到root权限。 sudo …

理解python中的变量

在 C 中&#xff0c;引用是某个变量的别名&#xff0c;一旦引用被初始化&#xff0c;就不能更改引用的目标。引用和原始变量指向的是同一个内存位置&#xff0c;不允许重新绑定。而 Python 的变量更像是指针或标签&#xff0c;可以在运行时重新指向不同的内存位置&#xff0c;即…