在HarmonyOS NEXT开发中使用Window时,做分屏切换时,回到自己的应用,关闭window重新弹window,子window的事件无法响应?

在HarmonyOS NEXT开发中使用Window时,做分屏切换时,回到自己的应用,关闭window重新弹window,子window的事件无法响应?自己应用弹出子window分屏B应用进入后台,关闭子window,回到自己应用,重新弹子window子window无法响应点击事件

阅读 1.2k
1 个回答

请参考以下代码:

EntryAbility.ets文件


import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
import { window } from '@kit.ArkUI'; 
 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
 
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
    AppStorage.setOrCreate("windowStage", windowStage); 
    windowStage.loadContent('pages/Index', (err) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); 
    }); 
  } 
 
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
 
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
 
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
}
Index.ets
import window from '@ohos.window'; 
 
@Entry 
@Component 
struct Index { 
  aboutToAppear() { 
    let windowStage_: window.WindowStage = AppStorage.get("windowStage") as window.WindowStage; 
    windowStage_.createSubWindow("subWindow", (err, win) => { //创建透明子窗口并打开 
      win.setUIContent('pages/Page1'); 
      win.resize(600, 600) 
      win.moveWindowTo(300, 300) 
      win.showWindow(); 
    }) 
  } 
  aboutToDisappear(){ 
    window.findWindow("subWindow").destroyWindow() 
  } 
  build() { 
    Row() { 
      Column() { 
        Button("子窗口弹窗") 
          .margin({ top: 20 }) 
          .onClick(() => { 
 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

Page1.ets文件

@Entry 
@Component 
struct Page1 { 
  @State message: string = 'Hello World'; 
 
  build() { 
    RelativeContainer() { 
      Text(this.message) 
        .id('Page1HelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: '__container__', align: VerticalAlign.Center }, 
          middle: { anchor: '__container__', align: HorizontalAlign.Center } 
        }) 
        .onClick(()=>{ 
          console.log('==============') 
        }) 
    } 
    .height('100%') 
    .width('100%') 
    .backgroundColor(Color.Pink) 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进