OpenHarmony鸿蒙开发学习:【Ability的启动模式】

Stage模型中,实现standard、singleton、specified多种模式场景。

本实例参考[开发指南]gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md。 本实例需要使用[aa工具] 查看应用Ability 模式信息。

效果预览

image

使用说明

image

或者直接添加
mau123789是v可以直接拿取鸿蒙文档

1、standard模式:

1)进入首页,点击番茄,会新建一个番茄的Ability,展示番茄的详情;

2)在番茄的详情界面,点击黄瓜,会新建一个黄瓜的Ability,展示黄瓜的详情;

3)使用aa工具查看Ability信息,此时存在以下Ability:1个番茄的Ability、1个黄瓜的Ability、1个首页的Ability;

2、singleton模式:

1)进入首页,点击冰淇凌,会新建一个冰淇凌的Ability,展示冰淇凌的详情;

2)在冰淇凌的详情界面,点击螃蟹,会复用冰淇凌的Ability,页面数据会刷新并展示螃蟹的详情;

3)使用aa工具查看Ability信息,此时存在以下Ability:1个冰淇凌的Ability、1个首页Ability;

3、specified模式:

1)进入首页,点击核桃,会新建一个核桃的Ability,展示核桃的详情;

2)在核桃的详情界面,点击蓝莓,会新建一个蓝莓的Ability,展示蓝莓的详情;

3)在蓝莓的详情界面,点击核桃,会复用已存在的核桃的Ability,实现specified模式下的单实例特性,页面数据会刷新并展示核桃的详情;

4)使用aa工具查看Ability信息,此时存在以下Ability:1个核桃的Ability、1个蓝莓的Ability、1个首页Ability;

具体实现

  • 本示例启动standard、singleton、specified三种模式的方法主要封装在Util当中,源码参考:[Util.ts]。
/*

 * Copyright (c) 2022 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import Logger from './Logger'



const TAG = '[Sample_StartMode]'

let contextCaller, want



interface EventHub {

  emit(event: string, ...args: Object[]): void

}



interface AbilityContext {

  eventHub: EventHub

}



export function getContextData(): any {

  Logger.info(TAG, 'UtilPage getContextData start')

  let context = getContext(this) as AbilityContext

  let data = {

    context: null,

    launchWant: null

  }

  context.eventHub.emit("getAbilityData", data)

  contextCaller = data.context // 拿到全局的context,即类似globalThis.mainAbilityContext

  want = data.launchWant

  Logger.info(TAG, 'UtilPage contextCaller ' + JSON.stringify(contextCaller))

  return { 'want': want }

}



export function startMode(wantParameters: any, abilityName: string) {

  Logger.info(TAG, `${abilityName} start`)

  getContextData()

  let want = {

    bundleName: 'ohos.samples.startmode',

    abilityName: abilityName,

    parameters: wantParameters

  }



  Logger.info(TAG, `${abilityName} contextCaller ${JSON.stringify(contextCaller)}`)

  contextCaller.startAbility(want).catch(err => {

    Logger.info(TAG, 'err is' + JSON.stringify(err))

  })

  Logger.info(TAG, `${abilityName} end`)

}



export function totast() {

  AlertDialog.show(

    {

      message: $r('app.string.totast'),

      secondaryButton: {

        value: 'ok',

        action: () => {

          Logger.info(TAG, 'Callback when the second button is clicked')

        }

      }

    }

  )

}
  • 新建Ability:创建三个代表standard、singleton、specified模式的Ability,如工程目录中的SingletonAbility、SpecifiedAbility、StandardAbility,并在module.json文件中将launchType属性修改为对应的启动模式属性。
  • 启动指定Ability:通过Util中的startMode函数根据页面所传的abilityName,启动对应的ability并进入详情页面。
  • specified多实例功能实现:specified模式则是根据MyAbilityStage中的onAcceptWant函数给用户返回一个ability标识,如果之前启动过标识的ability,不创建新的实例并拉回栈顶,否则创建新的实例并启动。
posted @   烧脑猴  阅读(113)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
鸿蒙NEXT文档资料 mau123789是助理v直接可拿取
点击右上角即可分享
微信分享提示