HarmonyOS bindSheet导致onBackPress回调链路被打破

bindSheet在覆写shouldDismiss后,Entry的onBackPress回调无法响应,不符合预期。

我们在onBackPress里统一管理了返回逻辑,可以不可以不阻断onBackPress回调?

HarmonyOS
2024-09-27 10:47:52
1503浏览
收藏 0
回答 2
回答 2
按赞同
/
按时间
鱼弦CTO
1

在 HarmonyOS 中,​​bindSheet​​​ 会覆盖默认的 ​​onBackPress​​​ 行为。如果需要在 ​​bindSheet​​​ 中保持对 ​​onBackPress​​ 的统一管理,可以手动处理返回逻辑。而不是依赖于默认的事件传播。

你可以通过手动设置 ​​shouldDismiss​​ 参数来控制是否应该关闭 ​​bindSheet​​,同时确保 ​​onBackPress​​ 事件能够被正确调用。在这种情况下,你需要重写 ​​onBackPress​​ 方法,并手动处理 ​​bindSheet​​ 的关闭操作。

### 示例代码

@Entry
@Component
struct MainPage {
  @State isShow: boolean = false
  @State sheetHeight: number = 200
  
  build() {
    Column() {
      Button("Open Sheet")
        .onClick(() => {
          this.isShow = true;
        })
        .fontSize(20)
        .margin(10)

      // Bind the sheet with custom dismiss logic
      .bindSheet($$this.isShow, this.sheetBuilder(), {  
        height: this.sheetHeight,
        shouldDismiss: (event) => {
          // Custom dismiss logic
          console.log('Custom shouldDismiss triggered');
          // Ensure that onBackPress can still be handled
          return true;  // Return true to allow the default dismiss behavior
        }
      })
    }
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .width('100%')
    .height('100%')
  }

  // Define the content of the sheet
  @Builder
  sheetBuilder() {
    Column() {
      Text("Sheet Content")
        .fontSize(20)
        .margin(10)
      
      Button("Close")
        .onClick(() => {
          this.isShow = false;
        })
        .fontSize(20)
        .margin(10)
    }
    .width('100%')
    .height('100%')
  }

  onBackPress() {
    // Custom back press handling
    console.log('MainPage onBackPress triggered');
    if (this.isShow) {
      this.isShow = false;
      return true;  // Indicate that the event has been handled
    }
    return false;  // Let the system handle the event if not handled
  }
}
  • 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.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

### 解释

  1. 自定义 onBackPress​:
  • 在​​MainPage​​ 组件中重写​​onBackPress​​ 方法,添加自定义返回逻辑。
  • 如果​​isShow​​ 为​​true​​,则关闭​​bindSheet​​ 并返回​​true​​ 表示事件已被处理。
  • 否则返回​​false​​,让系统继续处理该事件。
  1. 自定义 shouldDismiss​:
  • 在​​bindSheet​​ 配置中重写​​shouldDismiss​​ 方法,以确保在点击返回按钮时调用自定义返回逻辑。
  • 返回​​true​​ 以允许默认的关闭行为,但实际的关闭逻辑由​​onBackPress​​ 方法控制。
  1. 管理 bindSheet​ 状态
  • 使用​​@State​​ 管理​​isShow​​ 和​​sheetHeight​​ 状态。
  • 当点击打开按钮时,将​​isShow​​ 设置为​​true​​ 以显示​​bindSheet​​。
  • 当点击关闭按钮或触发自定义返回逻辑时,将​​isShow​​ 设置为​​false​​ 以关闭​​bindSheet​​。

通过上述方法,可以确保在使用 ​​bindSheet​​ 时,不会阻断 ​​onBackPress​​ 回调链路,同时仍然能够自定义 ​​bindSheet​​ 的关闭逻辑。如果有任何进一步的问题或特殊需求,请随时提问。

分享
微博
QQ
微信
回复
2024-09-28 16:16:53
superinsect

在关闭页面时才会触发onBackPress回调,就算不设置shouldDismiss,关闭bindsheet弹窗也不会触发onBackPress回调的。

分享
微博
QQ
微信
回复
2024-09-27 17:09:57


相关问题
HarmonyOS 视频抽帧上传的完成
1514浏览 • 1回复 待解决
WebView支持4G & WIFI双能力吗?
1501浏览 • 1回复 待解决
HarmonyOS 事件
1000浏览 • 1回复 待解决
HarmonyOS Watch没有
769浏览 • 1回复 待解决
HarmonyOS onNewWant未
600浏览 • 1回复 待解决
HarmonyOS Web组件
1156浏览 • 1回复 待解决
HarmonyOS onBackPress
358浏览 • 1回复 待解决
HarmonyOS 屏幕亮度变化
661浏览 • 1回复 待解决
HarmonyOS 人脸识别问题
801浏览 • 1回复 待解决
HarmonyOS onAreaChange方法问题
931浏览 • 1回复 待解决
HarmonyOS Slider值问题
706浏览 • 1回复 待解决
HarmonyOS 网络监听多次
836浏览 • 1回复 待解决
HarmonyOS 接口的写法
907浏览 • 1回复 待解决
HarmonyOS Grid拖拽无
564浏览 • 1回复 待解决
interface如何调用
1395浏览 • 1回复 待解决
Flutter - EventChannel问题
998浏览 • 1回复 待解决
HarmonyOS 活体检测问题
986浏览 • 1回复 待解决
HarmonyOS 首选项失效
788浏览 • 1回复 待解决