Stage模型中,實(shí)現(xiàn)standard、singleton、specified多種模式場景。
本實(shí)例參考[開發(fā)指南][gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]。 本實(shí)例需要使用[aa工具] 查看應(yīng)用Ability 模式信息。
效果預(yù)覽
使用說明
1、standard模式:
1)進(jìn)入首頁,點(diǎn)擊番茄,會新建一個(gè)番茄的Ability,展示番茄的詳情;
2)在番茄的詳情界面,點(diǎn)擊黃瓜,會新建一個(gè)黃瓜的Ability,展示黃瓜的詳情;
3)使用aa工具查看Ability信息,此時(shí)存在以下Ability:1個(gè)番茄的Ability、1個(gè)黃瓜的Ability、1個(gè)首頁的Ability;
2、singleton模式:
1)進(jìn)入首頁,點(diǎn)擊冰淇凌,會新建一個(gè)冰淇凌的Ability,展示冰淇凌的詳情;
2)在冰淇凌的詳情界面,點(diǎn)擊螃蟹,會復(fù)用冰淇凌的Ability,頁面數(shù)據(jù)會刷新并展示螃蟹的詳情;
3)使用aa工具查看Ability信息,此時(shí)存在以下Ability:1個(gè)冰淇凌的Ability、1個(gè)首頁Ability;
3、specified模式:
1)進(jìn)入首頁,點(diǎn)擊核桃,會新建一個(gè)核桃的Ability,展示核桃的詳情;
2)在核桃的詳情界面,點(diǎn)擊藍(lán)莓,會新建一個(gè)藍(lán)莓的Ability,展示藍(lán)莓的詳情;
3)在藍(lán)莓的詳情界面,點(diǎn)擊核桃,會復(fù)用已存在的核桃的Ability,實(shí)現(xiàn)specified模式下的單實(shí)例特性,頁面數(shù)據(jù)會刷新并展示核桃的詳情;
4)使用aa工具查看Ability信息,此時(shí)存在以下Ability:1個(gè)核桃的Ability、1個(gè)藍(lán)莓的Ability、1個(gè)首頁Ability;
具體實(shí)現(xiàn)
- 本示例啟動standard、singleton、specified三種模式的方法主要封裝在Util當(dāng)中,源碼參考:[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:創(chuàng)建三個(gè)代表standard、singleton、specified模式的Ability,如工程目錄中的SingletonAbility、SpecifiedAbility、StandardAbility,并在module.json文件中將launchType屬性修改為對應(yīng)的啟動模式屬性。
- 啟動指定Ability:通過Util中的startMode函數(shù)根據(jù)頁面所傳的abilityName,啟動對應(yīng)的ability并進(jìn)入詳情頁面。
- specified多實(shí)例功能實(shí)現(xiàn):specified模式則是根據(jù)MyAbilityStage中的onAcceptWant函數(shù)給用戶返回一個(gè)ability標(biāo)識,如果之前啟動過標(biāo)識的ability,不創(chuàng)建新的實(shí)例并拉回棧頂,否則創(chuàng)建新的實(shí)例并啟動。
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
57文章
2302瀏覽量
42689 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3635瀏覽量
16061
發(fā)布評論請先 登錄
相關(guān)推薦
評論