如下图所示,当Window弹窗不关闭的时候,跳转页面,通过侧滑手势返回无法触发页面的onBackPress进行页面返回:

问题代码示例参考如下:
import { window } from '@kit.ArkUI'; export class NYWindowDialog { private static windowDialog: NYWindowDialog; private dialogWindow: window.Window | undefined = undefined; private name: string = 'XX_WINDOW_DIALOG' private dialogConfig?: window.Configuration; private params: object = new Object(); private winBgColor: string = '#40000000' static getInstance(): NYWindowDialog { if (!NYWindowDialog.windowDialog) { NYWindowDialog.windowDialog = new NYWindowDialog() } return NYWindowDialog.windowDialog; } setParams(param: object): void { this.params = param } getParams(): object { return this.params } showWindowDialog(callback?: () => void): void { this.dialogConfig = { name: this.name, windowType: window.WindowType.TYPE_DIALOG } if (this.dialogWindow) { this.closeWindowDialog(); } window.createWindow(this.dialogConfig, (string, newWindow: window.Window) => { if (!newWindow) { return } this.dialogWindow = newWindow; newWindow.setWindowTouchable(true); newWindow.setUIContent(this.params['page']).then(() => { newWindow.setWindowBackgroundColor(this.winBgColor); }); newWindow.showWindow(() => { if (callback !== undefined) { callback(); } }); }); } closeWindowDialog(callback?: () => void): void { if (this.dialogWindow != null) { this.dialogWindow.destroyWindow(() => { if (callback !== undefined) { callback(); } }); } } setWindowBgColor(color: string): void { this.winBgColor = color } }
@ohos.window(窗口):窗口提供管理窗口的一些基础能力,包括对当前窗口的创建、销毁、各属性设置,以及对各窗口间的管理调度。可通过window.createWindow中的Configuration参数设置窗口属性。
从GIF图中可以看到,窗口打开后除了不能侧滑返回以外,点击事件依旧能返回上一个页面,
TYPE_DIALOG模态窗口页面设计是以模态方式显示,即在显示期间阻止与其他窗口的交互,故不支持侧滑操作。
onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/PageA', (err) => { if (err.code) { hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); return; } AppStorage.setOrCreate('windowStage', windowStage); // 储存窗口 hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); }); }
// PageA.ets页面 import window from '@ohos.window'; import * as subWin from './subWindow'; // 导入命名路由页面(子窗口) import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct PageA { @State windowStage: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage; build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('这是 page A') .fontSize(20) .margin({ bottom: 10 }); Button('点击打开子窗口') .onClick(() => { this.windowStage.createSubWindow('mySubWindow', (err, windowClass) => { try { windowClass.loadContentByName(subWin.entryName, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`); return; } console.info('Succeeded in loading the content.'); // 设置子窗口左上角坐标 windowClass.moveWindowTo(0, 0); // 展示子窗口 windowClass.showWindow(); // 设置子窗口全屏化布局避让安全区 windowClass.setWindowLayoutFullScreen(false); }); } catch (exception) { console.error(`Failed to load the content. Cause code: ${exception.code}, message: ${exception.message}`); } }); }); } .width('100%') .height('100%'); } }
// subWindow.ets页面 import { window } from '@kit.ArkUI'; export const entryName: string = 'subWindow'; @Entry({ routeName: entryName }) @Component export struct subWindow { @StorageProp('pageInfos') pageInfos: NavPathStack = new NavPathStack(); onBackPress(): boolean | void { // window.findWindow('mySubWindow').destroyWindow(); // 销毁子窗口 } build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('这是子窗口') .fontSize(20) .margin({ bottom: 10 }); Button('点击跳转 Page B') .onClick(() => { this.getUIContext().getRouter().pushUrl({ url: 'pages/PageB' }); }) .margin({ bottom: 15 }); Button('点击关闭子窗口') .onClick(() => { window.findWindow('mySubWindow').destroyWindow(); // 销毁子窗口 }); } .backgroundColor(Color.White) .width('100%') .height('100%'); } }
// PageB.ets页面 @Entry @Component struct pageB { build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Text('这是 Page B,请左滑') .fontSize(20) .margin({ bottom: 10 }); } .width('100%') .height('100%'); } }
以上页面需要新建为Page页面,若是新建的ArkTS File,需要将上述新建的页面,配置在entry/src/main/resources/base/profile中的main_pages.json文件中