HarmonyOS 如何拦截系统的返回事件?

在router开启的page中如何拦截系统的返回事件

阅读 575
1 个回答

通过onBackPress事件拦截,返回true表示页面自己处理返回逻辑

@Entry
@Component
struct IndexComponent {
  @State textColor: Color = Color.Black;

  onPageShow() {
    this.textColor = Color.Blue;
    console.info('IndexComponent onPageShow');
  }

  onPageHide() {
    this.textColor = Color.Transparent;
    console.info('IndexComponent onPageHide');
  }

  onBackPress() {
    this.textColor = Color.Red;
    console.info('IndexComponent onBackPress');
    return true
  }

  build() {
    Column() {
      Text('Hello World').fontColor(this.textColor).fontSize(30).margin(30)
    }.width('100%')
  }
}

参考链接 https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-custom-component-lifecycle-V5\#onbackpress

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进