HarmonyOS 子窗口如何阻止物理返回事件

当前页面上弹出了一个子窗口,如何做到屏幕边缘滑动不返回上一页,子窗口不确定在哪个页面弹出,所以不适合在onBackPressed里处理。

HarmonyOS
2024-08-29 09:28:34
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

尝试在子窗口的onBackPressed()内处理,不用管子窗口在哪个页面弹出,直接给子窗口加限制即可实现子窗口弹出状态时的禁止返回效果。

关于该诉求参考Demo如下:

onWindowStageCreate(windowStage: window.WindowStage): void { 
 // Main window is created, set main page for this ability 
 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
 
 windowStage.loadContent('pages/firstPage', (err) => { 
 if (err.code) { 
 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
 return; 
} 
//全局使用 
AppStorage.setOrCreate("windowStage", windowStage); 
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); 
}); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

Page1

import {window} from '@kit.ArkUI' 
import { common, Context } from '@kit.AbilityKit' 
 
 
@Entry 
@Component 
struct firstPage { 
 @State message: string = '-----------' 
 //全局使用 
 @State windowStage: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage 
 @State context: Context = getContext(this) as common.UIAbilityContext; 
 build() { 
  Row() { 
   Column() { 
    Text(this.message) 
     .fontSize(50) 
     .fontWeight(FontWeight.Bold) 
    Button(){ 
     Text("实验=Page2") 
    } 
    .width(80) 
    .height(60) 
    .onClick(()=>{ 
     this.message="实验已开始" 
     console.info(this.message) 
     this.windowStage.createSubWindow("mySubWindowPage2", (err, windowClass) => { 
      if (err.code > 0) { 
       console.error("failed to create subWindow Cause:" + err.message) 
       return; 
      } 
      // 设置子窗口加载页 
      try { 
       windowClass.setUIContent("pages/Page2", () => { 
        windowClass.setWindowBackgroundColor("#00000000") 
       }); 
       // 设置子窗口左上角坐标 
       windowClass.moveWindowTo(0, 200) 
       // 设置子窗口大小 
       windowClass.resize(vp2px(300), vp2px(300)) 
       // 展示子窗口 
       windowClass.showWindow(); 
       // 设置子窗口全屏化布局不避让安全区 
       windowClass.setWindowLayoutFullScreen(true); 
      } catch (err) { 
       console.error("failed to create subWindow Cause:" + err) 
      } 
     }) 
    }) 
    Button(){ 
     Text("实验=Page3") 
    } 
    .width(80) 
    .height(60) 
    .onClick(()=>{ 
     this.message="实验已开始" 
     console.info(this.message) 
     this.windowStage.createSubWindow("mySubWindowPage3", (err, windowClass) => { 
      if (err.code > 0) { 
       console.error("failed to create subWindow Cause:" + err.message) 
       return; 
      } 
      // 设置子窗口加载页 
      try { 
       windowClass.setUIContent("pages/Page3", () => { 
        windowClass.setWindowBackgroundColor("#00000000") 
       }); 
       // 设置子窗口左上角坐标 
       windowClass.moveWindowTo(0, 200) 
       // 设置子窗口大小 
       windowClass.resize(vp2px(300), vp2px(300)) 
       // 展示子窗口 
       windowClass.showWindow(); 
       // 设置子窗口全屏化布局不避让安全区 
       windowClass.setWindowLayoutFullScreen(true); 
      } catch (err) { 
       console.error("failed to create subWindow Cause:" + err) 
      } 
     }) 
    }) 
   } 
   .width('100%') 
  } 
  .height('100%') 
 } 
}
  • 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.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.

Page2

import { BusinessError } from '@kit.BasicServicesKit'; 
import {window} from '@kit.ArkUI' 
import { common } from '@kit.AbilityKit' 
 
@Entry 
@Component 
struct Page2 { 
 @State message: string = '这是page2' 
 @State windowStage: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage 
 @State context: Context = getContext(this) as common.UIAbilityContext; 
 build() { 
  Row() { 
   Column() { 
    Text(this.message) 
     .fontSize(30) 
     .fontWeight(FontWeight.Bold) 
    Button(){ 
     Text("隐藏") 
    } 
    .width(80) 
    .height(60) 
    .onClick(()=>{ 
     window.getLastWindow(getContext(this), (err, win) => { 
      win.destroyWindow() 
     }) 
    }) 
    Button(){ 
     Text("实验=Page3") 
    } 
    .width(80) 
    .height(60) 
    .onClick(()=>{ 
     this.message="实验已开始" 
     console.info(this.message) 
     this.windowStage.createSubWindow("mySubWindowPage3", (err, windowClass) => { 
      if (err.code > 0) { 
       console.error("failed to create subWindow Cause:" + err.message) 
       return; 
      } 
      // 设置子窗口加载页 
      try { 
       windowClass.setUIContent("pages/Page3", () => { 
        windowClass.setWindowBackgroundColor("#00000000") 
       }); 
       // 设置子窗口左上角坐标 
       windowClass.moveWindowTo(0, 200) 
       // 设置子窗口大小 
       windowClass.resize(vp2px(300), vp2px(300)) 
       // 展示子窗口 
       windowClass.showWindow(); 
       // 设置子窗口全屏化布局不避让安全区 
       windowClass.setWindowLayoutFullScreen(true); 
      } catch (err) { 
       console.error("failed to create subWindow Cause:" + err) 
      } 
     }) 
    }) 
   } 
   .width('100%') 
  } 
  .height('100%') 
  .backgroundColor(Color.Green) 
 } 
 onBackPressed() { 
  console.info("开始") 
  window.getLastWindow(this.context, (err: BusinessError, data) => { 
   const errCode: number = err.code; 
   if (errCode) { 
    console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err)); 
    return; 
   } 
   const windowClass = data; 
   windowClass.minimize((err: BusinessError) => { 
    const errCode: number = err.code; 
    if (errCode) { 
     console.error('Failed to minimize the window. Cause: ' + JSON.stringify(err)); 
     return; 
    } 
    console.info('Succeeded in minimizing the window.'); 
   }); 
   console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); 
  }); 
  console.info("结束") 
  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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.

Page3

import { window } from '@kit.ArkUI'; 
import { common } from '@kit.AbilityKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
@Entry 
@Component 
struct Page3 { 
 @State message: string = '这是page3' 
 @State windowStage: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage 
 @State context: Context = getContext(this) as common.UIAbilityContext; 
 build() { 
  Row() { 
   Column() { 
    Text(this.message) 
     .fontSize(30) 
     .fontWeight(FontWeight.Bold) 
    Button(){ 
     Text("隐藏") 
    } 
    .width(80) 
    .height(60) 
    .onClick(()=>{ 
     window.getLastWindow(getContext(this), (err, win) => { 
      win.destroyWindow() 
     }) 
    }) 
    Button(){ 
     Text("实验=Page2") 
    } 
    .width(80) 
    .height(60) 
    .onClick(()=>{ 
     this.message="实验已开始" 
     console.info(this.message) 
     this.windowStage.createSubWindow("mySubWindowPage2", (err, windowClass) => { 
      if (err.code > 0) { 
       console.error("failed to create subWindow Cause:" + err.message) 
       return; 
      } 
      // 设置子窗口加载页 
      try { 
       windowClass.setUIContent("pages/Page2", () => { 
        windowClass.setWindowBackgroundColor("#00000000") 
       }); 
       // 设置子窗口左上角坐标 
       windowClass.moveWindowTo(0, 200) 
       // 设置子窗口大小 
       windowClass.resize(vp2px(300), vp2px(300)) 
       // 展示子窗口 
       windowClass.showWindow(); 
       // 设置子窗口全屏化布局不避让安全区 
       windowClass.setWindowLayoutFullScreen(true); 
      } catch (err) { 
       console.error("failed to create subWindow Cause:" + err) 
      } 
     }) 
    }) 
   } 
   .width('100%') 
  } 
  .height('100%') 
  .backgroundColor(Color.Red) 
 } 
 onBackPressed() { 
  console.info("开始") 
  console.info("当前窗口有主窗口") 
  window.getLastWindow(this.context, (err: BusinessError, data) => { 
   const errCode: number = err.code; 
   if (errCode) { 
    console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err)); 
    return; 
   } 
   const windowClass = data; 
   windowClass.minimize((err: BusinessError) => { 
    const errCode: number = err.code; 
    if (errCode) { 
     console.error('Failed to minimize the window. Cause: ' + JSON.stringify(err)); 
     return; 
    } 
    console.info('Succeeded in minimizing the window.'); 
   }); 
   console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); 
  }); 
  console.info("结束") 
  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.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
分享
微博
QQ
微信
回复
2024-08-29 17:21:27
相关问题
HarmonyOS 窗口页面返回事件无效
570浏览 • 1回复 待解决
HarmonyOS 页面返回事件如何监听
833浏览 • 1回复 待解决
HarmonyOS RN如何拦截返回事件
533浏览 • 1回复 待解决
HarmonyOS 如何拦截物理返回
633浏览 • 1回复 待解决
HarmonyOS onClick事件如何阻止事件冒泡
947浏览 • 1回复 待解决
HarmonyOS 窗口跳转页面的返回问题
1014浏览 • 1回复 待解决
HarmonyOS 如何监听物理返回
697浏览 • 1回复 待解决
HarmonyOS 物理返回键监听
810浏览 • 1回复 待解决
HarmonyOS 侧滑返回事件拦截与绑定
2473浏览 • 1回复 待解决
如何监听手机“返回物理按键?
11467浏览 • 2回复 已解决
如何阻止Flex容器鼠标事件穿透
2924浏览 • 1回复 待解决
HarmonyOS 如何拖拽窗口
602浏览 • 1回复 待解决
HarmonyOS 监听手机物理返回
604浏览 • 1回复 待解决