HarmonyOS鸿蒙Next中浮窗返回主窗口时路由问题

HarmonyOS鸿蒙Next中浮窗返回主窗口时路由问题 我在应用中创建了一个浮窗,点击浮窗中加载页面中的按钮,期望在主窗口中打开对应的页面,但是新打开的页面却显示在浮窗中,不知道是不是不同窗口的事件循环不是同一个,请问怎样操作才能在主窗口中打开响应的页面,相应三个页面的示例代码如下

// index.ets
import window from '@ohos.window';

@Entry
@Component
struct Index {
  @State message: string = '入口页面';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        Button('打开浮窗', { type: ButtonType.Circle, stateEffect: true })
          .backgroundColor(0x317aff)
          .width(40)
          .height(40)
          .onClick(event => {
            this.createWindow().then(() => {

            }).catch(() => {

            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }

  async createWindow() {
    try {
      let windowClass: window.Window = await window.createWindow({
        name: 'FloatWindowTest',
        windowType: window.WindowType.TYPE_FLOAT,
        ctx: getContext(this)
      })

      await windowClass.moveWindowTo(100, 100)
      await windowClass.resize(300, 600)
      await windowClass.setUIContent('pages/FloatPage')
      await windowClass.showWindow()
      return true
    } catch (err) {
      console.error('Failed to create the floatWindow. Cause: ' + JSON.stringify(err));
      return Promise.reject(err)
    }
  }
}
// FloatPage.ets
import router from '@ohos.router'

@Entry
@Component
struct FloatPage {
  @State message: string = '浮窗页面'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        Button('打开详情页面', { type: ButtonType.Circle, stateEffect: true })
          .backgroundColor(0x317aff)
          .width(40)
          .height(40)
          .onClick(event => {
            router.pushUrl({
              url: 'pages/DetailedPage'
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
// DetailedPage.ets
@Entry
@Component
struct DetailedPage {
  @State message: string = '详情页面'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中浮窗返回主窗口时路由问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

请通过主窗口跳转

import router from '@ohos.router'

@Entry
@Component
struct FloatPage {
  @State message: string = '浮窗页面'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('打开详情页面', { type: ButtonType.Circle, stateEffect: true })
          .backgroundColor(0x317aff)
          .width(40)
          .height(40)
          .onClick((event) => {
            let windowStage = AppStorage.get("windowStage") as window.windowStage;
            windowStage.getMainWindowSync().getUIContent().getRouter().pushUrl({
              url: 'pages/DetailedPage'
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中浮窗返回主窗口时路由问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,浮窗返回主窗口时,路由问题主要涉及页面栈的管理和页面切换机制。鸿蒙系统采用基于Ability的路由机制,当浮窗返回主窗口时,系统会触发页面栈的回退操作,确保主窗口的页面能够正确显示。

具体来说,浮窗和主窗口分别对应不同的Ability或AbilitySlice。当浮窗关闭或返回主窗口时,系统会根据当前页面栈的状态进行路由处理。如果浮窗是通过startAbilitystartAbilityForResult启动的,返回时会触发onBackPressedonResult回调,系统会自动回退到上一个Ability或AbilitySlice。

在鸿蒙Next中,开发者可以通过AbilitySlicepresentterminate方法来管理浮窗的显示和关闭。当浮窗关闭时,系统会确保主窗口的页面栈恢复到之前的状态,避免出现路由混乱或页面丢失的情况。

此外,鸿蒙系统还提供了IntentOperation对象来管理页面跳转和参数传递。开发者可以通过这些对象精确控制浮窗和主窗口之间的路由逻辑,确保页面切换的流畅性和一致性。

总之,鸿蒙Next在处理浮窗返回主窗口的路由问题时,通过页面栈管理和Ability机制,确保页面切换的正确性和稳定性。开发者只需遵循系统的路由规则,即可实现流畅的页面切换体验。

在HarmonyOS鸿蒙Next中,浮窗返回主窗口时,路由管理需要特别关注。浮窗与主窗口属于不同的Ability或Page,返回时需确保主窗口的路由状态正确恢复。可通过AbilitySlicePage的生命周期方法(如onForeground)重新加载数据或更新UI。同时,使用Router模块进行页面跳转时,确保传递正确的参数和标志位,以避免路由冲突或数据丢失。建议在浮窗关闭前保存当前状态,并在主窗口恢复时重新应用。

回到顶部