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

embedded/2024/10/18 18:28:17/

生命周期(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/embedded/127751.html

相关文章

C++学习,容器类 <list>

C 标准库 <list> 是一个非常重要的容器类&#xff0c;用于存储元素集合&#xff0c;支持双向迭代器。<list>允许在容器的任意位置快速插入和删除元素。与数组或向量&#xff08;<vector>&#xff09;不同&#xff0c;<list> 不需要在创建时指定大小&am…

安卓14无法安装应用解决历程

客户手机基本情况&#xff1a; 安卓14&#xff0c;对应的 targetSdkVersion 34 前天遇到了安卓14适配问题&#xff0c;客户发来的截图是这样的 描述&#xff1a;无法安装我们公司的B应用。 型号&#xff1a;三星google美版 解决步骤&#xff1a; 1、寻找其他安卓14手机测试…

CMake变量:CMAKE_FIND_LIBRARY_SUFFIXES

CMAKE_FIND_LIBRARY_SUFFIXES是CMake中的一个变量&#xff0c;用于指定在查找库文件时使用的后缀列表。当CMake需要找到库文件时&#xff0c;它会尝试在这些后缀后添加库名来构建库文件的完整路径。例如&#xff0c;如果库名为mylib&#xff0c;并且CMAKE_FIND_LIBRARY_SUFFIXE…

【C++网络编程】(一)Linux平台下TCP客户/服务端程序

文章目录 Linux平台下TCP客户/服务端程序服务端客户端相关头文件介绍 Linux平台下TCP客户/服务端程序 图片来源&#xff1a;https://subingwen.cn/linux/socket/ 下面实现一个Linux平台下TCP客户/服务端程序&#xff1a;客户端向服务器发送&#xff1a;“你好&#xff0c;服务…

Unity 2d UI 实时跟随场景3d物体

2d UI 实时跟随场景3d物体位置&#xff0c;显示 3d 物体头顶信息&#xff0c;看起来像是场景中的3dUI&#xff0c;实质是2d UIusing System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.UI; /// <summary>…

matlab怎样将数据按行拼接和按列拼接(水平拼接竖直拼接)

1-将两个矩阵水平拼接 在 MATLAB 中&#xff0c;A [A, B] 是将矩阵 A 和矩阵 B 进行水平拼接的操作。具体来说&#xff0c;它是将矩阵 B 按照列的方式拼接到矩阵 A 的右边。 2-将两个矩阵竖直拼接 在 MATLAB 中&#xff0c;A [A&#xff1b; B] 是将矩阵 A 和矩阵 B 进行竖…

C#使用StructLayout特性来控制内存结构

C#在调用WInAPI函数时&#xff0c;可能会看到如下的声明 1 [StructLayout(LayoutKind.Sequential)] 2 public struct RECT 3 { 4 public int Left; 5 public int Top; 6 public int Right; 7 public int Bott…

【单例模式】

单例模式是指在内存中只会创建且仅创建一次对象的设计模式。 一、实现方式 1. 饿汉式 在类加载的时候就创建实例&#xff0c;无论是否使用&#xff0c;实例都会被创建。优点是实现简单&#xff0c;线程安全。缺点是可能造成资源浪费&#xff0c;而程序可能不一定会使用这个实例…