HarmonyOS 子窗口跳转页面的返回问题

一个Page A中弹出一个全屏的子窗口,子窗口中跳转到Page B页面,如何实现屏幕左滑返回可以返回上一个页面Page A,目前默认是退出应用到后台了。

HarmonyOS
2024-10-17 10:03:04
893浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考demo:

//EntryAbility.ets  
import { UIAbility } from '@kit.AbilityKit';  
import { window } from '@kit.ArkUI';  
export default class EntryAbility extends UIAbility {  
  onWindowStageCreate(windowStage: window.WindowStage): void {  
    windowStage.loadContent("pages/pageA", (err, data) => {  
      //全局使用  
      AppStorage.setOrCreate("windowStage", windowStage);  
    });  
  }  

//pageA.ets  
import window from '@ohos.window'  
import { common, Context } from '@kit.AbilityKit'  
import * as subWin from '../pages/subWindow'; // 导入命名路由页面(子窗口)  
import { BusinessError } from '@kit.BasicServicesKit';  
@Entry  
@Component  
struct pageA {  
  //全局使用  
  @State windowStage: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage  
  @State context: Context = getContext(this) as common.UIAbilityContext;  
  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}`);  
          }  
        })  
      })  
    }  
    .backgroundColor(Color.Pink).width('100%') .height('100%')  
  }  
}
//subWindow.ets  
import { router } from '@kit.ArkUI';  
export const entryName: string = 'subWindow';  
@Entry({ routeName: entryName })  
@Component  
struct subWindow {  
  build() {  
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {  
      Text('这是 子窗口').fontSize(20).margin({bottom:10})  
      Button('点击跳转 Page B')  
        .onClick(() => {  
          router.pushUrl({  
            url: 'pages/pageB'  
          })  
        })  
    }.backgroundColor(Color.Yellow).width('100%') .height('100%')  
  }  
}
//pageB.ets  
import { window } from '@kit.ArkUI'  
@Entry  
@Component  
struct pageB {  
  build() {  
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {  
      Text('这是 Page B,请左滑').fontSize(20).margin({bottom:10}).fontColor(Color.White)  
    }.backgroundColor(Color.Green).width('100%') .height('100%')  
  }  
  onBackPress() {//返回时销毁子窗口  
    window.findWindow("mySubWindow").destroyWindow()  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-17 15:51:25


相关问题
HarmonyOS 窗口页面返回事件无效
434浏览 • 1回复 待解决
HarmonyOS 窗口路由切换问题
601浏览 • 1回复 待解决
HarmonyOS 窗口弹出popup问题
478浏览 • 1回复 待解决
HarmonyOS 创建窗口后相关问题
384浏览 • 1回复 待解决
HarmonyOS如何实现hap包页面的跳转
957浏览 • 1回复 待解决
HarmonyOS 跳转到系统设置页面的方法
1035浏览 • 1回复 待解决
HarmonyOS page页面的问题
687浏览 • 1回复 待解决
窗口加载的页面是否可以带参数
701浏览 • 1回复 待解决
如何实现一个页面显示窗口
1184浏览 • 1回复 待解决
路由实现动态页面的跳转方案
2428浏览 • 1回复 待解决
HarmonyOS 跳转页面问题
975浏览 • 1回复 待解决
HarmonyOS 如何拖拽窗口
396浏览 • 1回复 待解决