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.'); 
}); 
}

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%') 
 } 
}

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; 
 } 
}

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; 
 } 
}
分享
微博
QQ
微信
回复
2024-08-29 17:21:27
相关问题
HarmonyOS RN如何拦截返回事件
74浏览 • 1回复 待解决
HarmonyOS 窗口跳转页面的返回问题
262浏览 • 1回复 待解决
HarmonyOS 物理返回键监听
49浏览 • 1回复 待解决
HarmonyOS 侧滑返回事件拦截与绑定
1258浏览 • 1回复 待解决
如何监听手机“返回物理按键?
10271浏览 • 2回复 已解决
如何阻止Flex容器鼠标事件穿透
2014浏览 • 1回复 待解决
如何通过代码关闭窗口
422浏览 • 1回复 待解决