HarmonyOS onBackPressed不触发

在NavDestination中,监听onBackPressed不触发,无论是写在build里还是外面。

@Component
@Entry
export struct CancelSuccessPage {
  build() {
    NavDestination() {
      Column() {
        Column() {
          Image($r('app.media.ic_success_apply'))
            .width(selfLpx(64))
            .height(selfLpx(64))
            .margin({ top: selfLpx(69) })
          Text('注销账号申请通过')
            .fontColor('#ff201d31')
            .fontSize(selfLpx(16))
            .margin({ top: selfLpx(14) })
          Text('您的注销账号申请已受理,我们会在1-10工作日内完成账号注销,账号注销未完成之前,您还可以正常使用当前账号,账号注销完成后,您的账号将无法找回且不可恢复,若您想正常使用APP的所有功能需要重新注册登录,谢谢!')
            .fontColor('#ff7b7d85')
            .fontSize(selfLpx(14))
            .margin({
              top: selfLpx(8),
              left: selfLpx(24),
              right: selfLpx(24),
              bottom: selfLpx(30)
            })
        }
        .backgroundColor(Color.White)

        Button('返回首页')
          .width(DpUtils.ratio(343))
          .height(DpUtils.ratio(40))
          .backgroundColor('#FF620D')
          .margin({ top: selfLpx(15) })
          .fontColor('#ffffff')
          .fontSize(DpUtils.ratio(17))
          .onClick(() => {
            RouterManager.pushRoot()
          })
      }
    }.backgroundColor('#F2F4F7')
    .onBackPressed(() => {
      LogUtils.error('监听返回事件')
      RouterManager.pushRoot()
      return true
    })
  }

  onBackPress(): boolean | void {
    LogUtils.error('监听返回事件')
    RouterManager.pushRoot()
    return true
  }
}
  • 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.
HarmonyOS
2025-01-09 15:51:55
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考示例如下:

@Builder
export function PageTwoBuilder(name: string, param: Object) {
  PageHarB()
}

@Component
export struct PageHarB {
  @State message: string = 'Hello PageHarB';
  @State pathStack: NavPathStack = new NavPathStack()

  build() {
    NavDestination() {
      Row() {
        Column() {
          Text(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .onClick(() => {
              console.info("har A page2")
            })
        }
        .width('100%')
      }
      .height('100%')
    }
    .onBackPressed(() => {
      this.pathStack.pop()
      console.log('bb')
      return true
    })
  }
}
  • 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.

.onBackPressed监听的是左上角的返回箭头.

分享
微博
QQ
微信
回复
2025-01-09 19:06:09


相关问题
HarmonyOS onKeyEvent 按钮触发
610浏览 • 1回复 待解决
HarmonyOS navigation触发onpageshow
534浏览 • 1回复 待解决
HarmonyOS 生命周期触发
656浏览 • 1回复 待解决
返回页面时触发aboutToAppear
4718浏览 • 1回复 待解决
HarmonyOS TabContent页面生命周期触发
1266浏览 • 1回复 待解决