Stage模型中,实现standard、singleton、specified多种模式场景。
本实例参考[开发指南][gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]。 本实例需要使用[aa工具] 查看应用Ability 模式信息。
效果预览
使用说明
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,不创建新的实例并拉回栈顶,否则创建新的实例并启动。
审核编辑 黄宇
-
鸿蒙
+关注
关注
57文章
2398浏览量
43133 -
OpenHarmony
+关注
关注
25文章
3753浏览量
16722
发布评论请先 登录
相关推荐
OpenHarmony源码编译后烧录镜像教程,RK3566鸿蒙开发板演示
![<b class='flag-5'>OpenHarmony</b>源码编译后烧录镜像教程,RK3566<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>板演示](https://file1.elecfans.com/web3/M00/04/41/wKgZO2dyAH2AFFx6AAB2FN45Lh8124.png)
OpenHarmony怎么修改DPI密度值?触觉智能RK3566鸿蒙开发板演示
![<b class='flag-5'>OpenHarmony</b>怎么修改DPI密度值?触觉智能RK3566<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>板演示](https://file1.elecfans.com/web3/M00/03/8B/wKgZPGdqLoyAKZgAAAB1wHV_XdE820.png)
如何在开源鸿蒙OpenHarmony开启SELinux模式?RK3566鸿蒙开发板演示
![如何在开源<b class='flag-5'>鸿蒙</b><b class='flag-5'>OpenHarmony</b>开启SELinux<b class='flag-5'>模式</b>?RK3566<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>板演示](https://file1.elecfans.com/web2/M00/0C/C1/wKgaomc7Hr6AL1UtAAB6Pp_Ckc0170.png)
鸿蒙OpenHarmony南向/北向快速开发教程-迅为RK3568开发板
鸿蒙开发:启动本地PageAbility
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>:<b class='flag-5'>启动</b>本地PageAbility](https://file1.elecfans.com/web2/M00/C5/D5/wKgZomYCnRSAcOMcAACEEnttXHc600.png)
鸿蒙Ability Kit(程序框架服务)【UIAbility组件启动模式】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>Ability</b> Kit(程序框架服务)【UIAbility组件<b class='flag-5'>启动</b><b class='flag-5'>模式</b>】](https://file1.elecfans.com/web2/M00/EB/9D/wKgaomZZxTSANfBpAGBNtLqJkcU253.jpg)
哪吒汽车APP启动鸿蒙原生应用开发
鸿蒙开发接口Ability框架:【AbilityDelegator】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>接口<b class='flag-5'>Ability</b>框架:【AbilityDelegator】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
鸿蒙开发接口Ability框架:【(AbilityDelegator)】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>接口<b class='flag-5'>Ability</b>框架:【(AbilityDelegator)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
鸿蒙开发接口Ability框架:【@ohos.ability.particleAbility (particleAbility模块)】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>接口<b class='flag-5'>Ability</b>框架:【@ohos.<b class='flag-5'>ability</b>.particleAbility (particleAbility模块)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
鸿蒙开发接口Ability框架:【@ohos.ability.featureAbility (FeatureAbility模块)】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>接口<b class='flag-5'>Ability</b>框架:【@ohos.<b class='flag-5'>ability</b>.featureAbility (FeatureAbility模块)】](https://file1.elecfans.com/web2/M00/E0/AF/wKgZomY4loOAFF3hAACaC2ijUOY058.png)
鸿蒙开发接口Ability框架:【@ohos.application.Ability (Ability)】
![<b class='flag-5'>鸿蒙</b><b class='flag-5'>开发</b>接口<b class='flag-5'>Ability</b>框架:【@ohos.application.<b class='flag-5'>Ability</b> (<b class='flag-5'>Ability</b>)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
评论