目录

弹窗无法通过侧滑关闭

问题现象

侧滑无法关闭弹窗,侧滑时无响应或是主页面返回上一级。

背景知识

问题定位

[h2]场景一

子窗口侧滑无响应。

  1. 侧滑时输出如下日志,从日志可知消费侧滑事件的窗口不是主窗口,这种窗口是通过windowStage.createSubWindow创建的子窗口。
    I     [(-1:100001:active)] [com.example.myapplication][entry][100001]: OnBackPressed called
    W     [(-2:100001:active)] Fail to find subwindow in subwindowMap_, instanceId is 100001.
    E     (82)GetSingleton: can not get OHOS::Rosen::WindowInfoReporter
    I     (2888)PerformBack: PerformBack is not MainWindow, return
  2. 检查子窗口是否重写onBackPress方法主动销毁子窗口。

[h2]场景二

弹窗组件侧滑无响应。

  1. 日志过滤backPress,看到侧滑事件是被Overlay消费的,即叠加层,一般是一些弹窗组件。
    [(-1:100000:singleton)] [com.example][entry][100000]: OnBackPressed called
    [(100000:100000:scope)] Overlay consumed backpressed event
  2. 排查弹窗组件是否注册onWillDismiss回调函数,检查拦截逻辑,看到应用只对下拉操作进行了处理。
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      // 下拉
      if (dismissDialogAction.reason == DismissReason.SLIDE_DOWN) {
        // 关闭弹窗
        dismissDialogAction.dismiss()
      }
    }

    关闭原因类型:

    名称

    值

    描述

    PRESS_BACK

    0

    点击三键back、左滑/右滑、键盘ESC。

    TOUCH_OUTSIDE

    1

    点击遮障层时。

    CLOSE_BUTTON

    2

    点击关闭按钮。

    SLIDE_DOWN

    3

    下拉关闭。该接口仅支持在半模态转场中使用。

[h2]场景三

侧滑未关闭弹出框,而是主页面返回上一级。
  1. 在日志中搜索onBackPress,可以看到onBackPress return true,说明应用重写了onBackPress方法。
  2. 通过DevEco Testing查看页面,发现页面由JsView构成。商品页和评论区在同一个根节点上。

  3. 在侧滑关闭弹窗前,通过日志发现焦点聚焦在了Column下的子节点,此时侧滑逻辑由主页面的onBackPress方法确定。因此是主页面onBackPress方法内逻辑错误,未优先关闭弹出框而是返回了上一级。
    12-16 15:06:11.905   64077-64077   C0391C/应用包名/AceFocus         应用包名     I     [(100001:100001:scope)] Lost focus to view root: Column
    12-16 15:06:11.905   64077-64077   C0391C/应用包名/AceFocus         应用包名     I     [(100001:100001:scope)] FocusSwitch end, Column/secure_field onBlur, Column/secure_field onFocus, start: 3, end: 1, update: 0
    12-16 15:06:11.905   64077-64077   C03933/应用包名/AceKeyboard      应用包名     I     [(100001:100001:scope)] current focus node: (Column/14). isDynamic: 0

分析结论

[h2]场景一

因为未在子窗口重写onBackPress,应用会执行默认的路由返回逻辑,由于子窗口没有可返回的上一页,所以无响应。

[h2]场景二

弹窗组件注册了onWillDismiss回调函数,然而未对侧滑场景做处理。

[h2]场景三

焦点聚焦在主页面,而主页面重写的onBackPress方法内逻辑错误,未优先关闭弹出框而是返回了上一级。

修改建议

[h2]场景一

在onBackPress方法中,调用destroyWindow主动销毁子窗口。

[h2]场景二

对侧滑场景DismissReason.PRESS_BACK也进行关闭弹窗操作。

@CustomDialog
struct CustomDialogExample {
  controller?: CustomDialogController;
  cancel: () => void = () => {
  };
  confirm: () => void = () => {
  };

  build() {
    Column() {
      Text('这是自定义弹窗')
        .fontSize(30)
        .height(100)
      Button('关闭弹窗')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close();
          }
        })
        .margin(20)
    }
  }
}

@Entry
@Component
struct CustomDialogPage {
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: () => {
        this.onCancel();
      },
      confirm: () => {
        this.onAccept();
      }
    }),
    cancel: this.existApp,
    autoCancel: true,
    onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
      console.info(`reason= ${dismissDialogAction.reason}`);
      console.info('dialog onWillDismiss');
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss();
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss();
      }
    },
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: -20 },
    customStyle: false,
    cornerRadius: 20,
    width: 300,
    height: 200,
    borderWidth: 1,
  });

  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    this.dialogController = null; // 将dialogController置空
  }

  onCancel() {
    console.info('Callback when the first button is clicked');
  }

  onAccept() {
    console.info('Callback when the second button is clicked');
  }

  existApp() {
    console.info('Click the callback in the blank area');
  }

  build() {
    Column() {
      Button('click me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open();
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

[h2]场景三

修改onBackPress逻辑,在存在弹出框时优先关闭弹出框。

@Entry
@Component
struct MainPage {
  @State isShowDialog: boolean = false;
  @State isShowPage1: boolean = true;

  onBackPress(): boolean | void {
    if (this.isShowDialog) { // 如果已打开弹出框,则优先关闭弹出框
      this.isShowDialog = false;
      return true;
    } else if (!this.isShowPage1) { // 如果在第二个页面,则返回第一个页面
      this.isShowPage1 = true;
      return true;
    }
    return false;
  }

  build() {
    Stack() {
      if (this.isShowPage1) {
        Page1({ isShowPage1: this.isShowPage1 })
      } else {
        Page2({ isShowDialog: this.isShowDialog })
      }
      if (this.isShowDialog) {
        Dialog()
      }
    }
    .width('100%')
    .height('100%')
  }
}

@Component
struct Page1 {
  @Link isShowPage1: boolean;

  build() {
    Stack() {
      Text('页面一')
        .fontSize(22)
        .margin({ bottom: 100 })
      Button('点击跳转页面二')
        .onClick(() => {
          this.isShowPage1 = false;
        })
    }
    .width('100%')
    .height('100%')
  }
}

@Component
struct Page2 {
  @Link isShowDialog: boolean;

  build() {
    Stack() {
      Text('页面二')
        .fontSize(22)
        .margin({ bottom: 100 })
      Button('点击弹出弹出框')
        .onClick(() => {
          this.isShowDialog = true;
        })
    }
    .width('100%')
    .height('100%')
  }
}

@Component
struct Dialog {
  build() {
    Stack() {
      Text('这是弹出框')
        .fontSize(20)
        .fontColor(Color.White)
    }
    .backgroundColor('#383838')
    .opacity(0.9)
    .width('70%')
    .height('24%')
  }
}