鸿蒙 从打开一个新窗口到Stage模型的UIAbility组件

打开一个新的窗口

我们首先来实现如何在一个应用中打开一个新窗口,使用的模型是 Stage 模型

  1. 在项目文件里,新建一个 newWindow.ets 新文件
    src/main/ets/pages/newWindow.ets

newWindow.ets文件里面随便写点什么都行,这里是第一步创建的文件

新建的文件可以在 src/main/resources/base/profile/main_pages.json 查看

  1. 新建一个 Ability

src/main/ets 文件=>鼠标右键=>新建=>Ability=>写好新的Ability名字比如这里我写newWindow

  1. ets 文件夹下会出现一个新的 newWindow 文件夹,里面有个新的 newWindow.ets 文件,打开它并修改下面的生命周期函数确保启动文件为第一步新建的文件newWindow.ets
ts 复制代码
onWindowStageCreate(windowStage: window.WindowStage): void {
    windowStage.loadContent('pages/newWindow')
  }
  1. 回到入口文件src/main/ets/pages/Index.ets,加入一个可以跳到新窗口的按钮
ts 复制代码
import { common } from '@kit.AbilityKit'

@Entry
@Component
struct Index {
  build() {
    Button('在新窗口打开')
      .onClick(_ => {
        //getContext() 得到UIAbilityContext对象
        //得到页面所在UIAbility对应的UIAbilityContext
        // 强行逆转父类 as
        let ctx = getContext() as common.UIAbilityContext

        //启动一个新的UIAbility
        ctx.startAbility({
          bundleName: 'com.example.myapplication', //必需!要启动哪个应用下的UIAbility
          abilityName: 'newWindow', //必需!要启动的UIAbility名(参考moduels.json5中的名字)
          // moduleName: 'entry', //可选的!如果目标Ability在同一个模块下,此属性可以省略
        })
      })
  }
}
// AppScope/app.json5 查看 bundleName

这样就可以打开一个新的窗口了

使用 Want

显式 Want

ts 复制代码
// 我这里又新建了一个项目,所以有些名字和上面的例子不同
import { common ,Want } from '@kit.AbilityKit'

@Entry
@Component
struct Index {
  build() {
    Button('在新窗口打开')
      .onClick(_ => {
        let ctx = getContext() as common.UIAbilityContext

        let wantInfo: Want = {
          bundleName: 'com.example.quanguokefei',
          abilityName: 'Page1',
          moduleName: 'entry',
        }
        //启动一个新的UIAbility
        ctx.startAbility(wantInfo)
      })
  }
}

隐式 Want

  1. 跳到自定义窗口
    隐式 Want 使用 skills 标签来定义需要使用的能力
  • 先去目录 src/main/module.json5修改需要跳转的窗口
  • 比如我要跳转的窗口是 Page1,加入 actions: ["iampage1"],名字自由取,可以多个
json5 复制代码
{
  name: "Page1",
  srcEntry: "./ets/page1/Page1.ets",
  description: "$string:Page1_desc",
  icon: "$media:layered_image",
  label: "$string:Page1_label",
  startWindowIcon: "$media:startIcon",
  startWindowBackground: "$color:start_window_background",
  skills: [
    {
      actions: ["iampage1", "other_name"],
    },
  ],
}

处理 Want

ts 复制代码
import { common ,Want } from '@kit.AbilityKit'

@Entry
@Component
struct Index {
  build() {
    Button('在新窗口打开')
      .onClick(_ => {
        let ctx = getContext() as common.UIAbilityContext
        // 在这里只需要action就行
        let wantInfo: Want = {
          action:'iampage1'
        }
        ctx.startAbility(wantInfo)
      })
  }
}

窗口启动模式

singleton 启动模式为单实例模式,也是默认情况下的启动模式
multiton 启动模式为多实例模式
specified 启动模式为指定实例模式,针对一些特殊场景使用(例如文档应用中每次新建文档希望都能新建一个文档实例,重复打开一个已保存的文档希望打开的都是同一个文档实例)

src/main/module.json5 文件中修改

ts 复制代码
{
  "module": {
    "abilities": [
      {
        "name": "Page1Ability",
        "launchType": "singleton | multiton | specified"
      }
    ]
  }
}

UIAbility 生命周期

执行顺序:

首次启动 onCreate → onWindowStageCreate → onForeground

切换到后台 onBackground

从后台重新打开 onForeground

正常退出应用 onBackground → onWindowStageDestroy → onDestroy

窗口已经存在的情况下,从主程序进入,不是窗口视图 onNewWant → onForeground

相关推荐
anyup13 小时前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
Ranger092918 小时前
鸿蒙开发新范式:Gpui
rust·harmonyos
Huang兄19 小时前
鸿蒙-深色模式适配
harmonyos·arkts·arkui
SummerKaze3 天前
为鸿蒙开发者写一个 nvm:hmvm 的设计与实现
harmonyos
在人间耕耘4 天前
HarmonyOS Vision Kit 视觉AI实战:把官方 Demo 改造成一套能长期复用的组件库
人工智能·深度学习·harmonyos
王码码20354 天前
Flutter for OpenHarmony:socket_io_client 实时通信的事实标准(Node.js 后端的最佳拍档) 深度解析与鸿蒙适配指南
android·flutter·ui·华为·node.js·harmonyos
HarmonyOS_SDK4 天前
【FAQ】HarmonyOS SDK 闭源开放能力 — Ads Kit
harmonyos
Swift社区5 天前
如何利用 ArkUI 框架优化鸿蒙应用的渲染性能
华为·harmonyos
特立独行的猫a5 天前
uni-app x跨平台开发实战:开发鸿蒙HarmonyOS影视票房榜组件完整实现过程
华为·uni-app·harmonyos·轮播图·uniapp-x
盐焗西兰花5 天前
鸿蒙学习实战之路-STG系列(5/11)-守护策略管理-添加与修改策略
服务器·学习·harmonyos