HarmonyOS @entry 修饰的页面,onBackPress方法不触发

使用动态路由方案统一管理了页面路由,使用过程中发现, 子页面无法触发自己页面中的 onBackPress 方法, 只能触发 NavDestination 的 onBackPressed 方

主页面代码:

build() {
  Navigation(this.pageStack) {
    Text(this.message)
      .id('HelloWorld')
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .alignRules({
        center: { anchor: '__container__', align: VerticalAlign.Center },
        middle: { anchor: '__container__', align: HorizontalAlign.Center }
      })
      .onClick(() => {
        // DynamicsRouter.pushUri("login/MainPage")
        DynamicsRouter.pushUri("web/WebPage", { params: "resource://rawfile/test_web.html" })
      })
  }
  .height('100%')
  .width('100%')
  .navDestination(this.pageMap)
  .mode(NavigationMode.Stack)
}

@Builder
pageMap(name: string, param: ESObject) {
  // 显示标题
  NavDestination() {
    // 根据模块名,获取WrappedBuilder对象,通过builder接口创建页面
    DynamicsRouter.getBuilder(name).builder(param);
  }
  .onBackPressed(()=>{
    console.info("main onBackPressed")
    return false
  })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
HarmonyOS
2024-12-24 16:34:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

NavDestination本身就不会触发onbackpress,推荐的方案是可以在aboutToDisappear生命周期中调用下onBackPress,代码如下:

import { AppRouter, DynamicsRouter } from '@weimai/router';

@AppRouter({ name: "library/MainPage" })
@Component
export struct MainPage {
  @State message: string = 'library/MainPage';

  aboutToAppear(): void {

    console.info("子模块", "MainPage", "aboutToAppear >>>")
  }
  aboutToDisappear(): void {
    console.log('3333')
    this.onBackPress()

  }

  onPageShow(): void {
    console.log('3333')
  }

  onPageHide(): void {
    console.log('776767')
    this.onBackPress()
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        Button("back")
          .id("next")
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
          .align(Alignment.Center)
          .onClick(() => {
            console.log("子模块", "点击", "返回到上页面")
            DynamicsRouter.popAppRouter()
          })
          .margin({ top: 20 })
      }
      .width('100%')
    }
    .height('100%')
  }

  onBackPress(): boolean | void {
    console.info("子模块", "onBackPress", "触发了")
    return false
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
分享
微博
QQ
微信
回复
2024-12-24 17:32:30
相关问题
返回页面触发aboutToAppear
4678浏览 • 1回复 待解决
HarmonyOS TabContent页面生命周期触发
1238浏览 • 1回复 待解决
HarmonyOS onBackPressed触发
477浏览 • 1回复 待解决