啟動指定頁面
當PageAbility的啟動模式設置為單例時(具體設置方法和典型場景示例見[PageAbility的啟動模式],缺省情況下是單實例模式),若PageAbility已被拉起,再次啟動PageAbility會觸發onNewWant回調(即非首次拉起)。應用開發者可以通過want傳遞啟動參數,例如開發者希望指定頁面啟動PageAbility,可以通過want中的parameters參數傳遞pages信息,具體示例代碼如下: 開發前請熟悉鴻蒙開發指導文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
調用方PageAbility的app.ets中或者page中,使用startAbility再次拉起PageAbility,通過want中的uri參數傳遞頁面信息:
import featureAbility from '@ohos.ability.featureAbility';
import Want from '@ohos.app.ability.Want';
import hilog from '@ohos.hilog';
const TAG: string = 'PagePageAbilityFirst';
const domain: number = 0xFF00;
(async (): Promise< void > = > {
let wantInfo: Want = {
bundleName: 'com.samples.famodelabilitydevelop',
abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton',
parameters: { page: 'pages/second' }
};
featureAbility.startAbility({ want: wantInfo }).then((data) = > {
hilog.debug(domain, TAG, `restartAbility success : ${data}`);
});
})()
在目標端PageAbility的onNewWant回調中獲取包含頁面信息的want參數:
// GlobalContext.ts 構造單例對象
export class GlobalContext {
private constructor() {
}
private static instance: GlobalContext;
private _objects = new Map< string, Object >();
public static getContext(): GlobalContext {
if (!GlobalContext.instance) {
GlobalContext.instance = new GlobalContext();
}
return GlobalContext.instance;
}
getObject(value: string): Object | undefined {
return this._objects.get(value);
}
setObject(key: string, objectClass: Object): void {
this._objects.set(key, objectClass);
}
}
import Want from '@ohos.app.ability.Want';
import featureAbility from '@ohos.ability.featureAbility';
import { GlobalContext } from '../utils/GlobalContext';
class PageAbilitySingleton {
onNewWant(want: Want) {
featureAbility.getWant().then((want) = > {
GlobalContext.getContext().setObject('newWant', want);
})
}
}
export default new PageAbilitySingleton();
在目標端頁面的自定義組件中獲取包含頁面信息的want參數并根據uri做路由處理:
import Want from '@ohos.app.ability.Want';
import router from '@ohos.router';
import { GlobalContext } from '../../utils/GlobalContext';
@Entry
@Component
struct First {
onPageShow() {
let newWant = GlobalContext.getContext().getObject('newWant') as Want;
if (newWant) {
if (newWant.parameters) {
if (newWant.parameters.page) {
router.pushUrl({ url: newWant.parameters.page as string});
GlobalContext.getContext().setObject("newWant", undefined)
}
}
}
}
build() {
Column() {
Row() {
Text($r('app.string.singleton_first_title'))
.fontSize(24)
.fontWeight(FontWeight.Bold)
.textAlign(TextAlign.Start)
.margin({ top: 12, bottom: 11, right: 24, left: 24 })
}
.width('100%')
.height(56)
.justifyContent(FlexAlign.Start)
Image($r('app.media.pic_empty'))
.width(120)
.height(120)
.margin({ top: 224 })
Text($r('app.string.no_content'))
.fontSize(14)
.margin({ top: 8, bottom: 317, right: 152, left: 152 })
.fontColor($r('app.color.text_color'))
.opacity(0.4)
}
.width('100%')
.height('100%')
.backgroundColor($r('app.color.backGrounding'))
}
}
當PageAbility的啟動模式設置為多實例模式或為首次啟動單例模式的PageAbility時(具體設置方法和典型場景示例見[PageAbility的啟動模式]),在調用方PageAbility中,通過want中的parameters參數傳遞要啟動的指定頁面的pages信息,調用startAbility()方法啟動PageAbility。被調用方可以在onCreate中使用featureAbility的getWant方法獲取want,再通過調用router.pushUrl實現啟動指定頁面。
調用方的頁面中實現按鈕點擊觸發startAbility方法啟動目標端PageAbility,startAbility方法的入參want中攜帶指定頁面信息,示例代碼如下:
import featureAbility from '@ohos.ability.featureAbility';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
import fs from '@ohos.file.fs';
import promptAction from '@ohos.promptAction';
import worker from '@ohos.worker';
import hilog from '@ohos.hilog';
const TAG: string = 'PagePageAbilityFirst';
const domain: number = 0xFF00;
@Entry
@Component
struct PagePageAbilityFirst {
build() {
Column() {
//...
List({ initialIndex: 0 }) {
//...
ListItem() {
Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
//...
}
.onClick(() = > {
let want: Want = {
bundleName: 'com.samples.famodelabilitydevelop',
abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard',
parameters: { page: 'pages/first' }
};
featureAbility.startAbility({ want: want }).then((data) = > {
hilog.info(domain, TAG, `startAbility finish:${data}`);
}).catch((err: BusinessError) = > {
hilog.info(domain, TAG, `startAbility failed errcode:${err.code}`);
})
})
}
//...
ListItem() {
Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
//...
}
.onClick(() = > {
let want: Want = {
bundleName: 'com.samples.famodelabilitydevelop',
abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard',
parameters: { page: 'pages/second' }
};
featureAbility.startAbility({ want: want }).then((data) = > {
hilog.info(domain, TAG, `startAbility finish:${data}`);
}).catch((err: BusinessError) = > {
hilog.info(domain, TAG, `startAbility failed errcode:${err.code}`);
})
})
}
//...
}
//...
}
//...
}
}
`HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`
目標端PageAbility的onCreate生命周期回調中通過featureAbility的getWant方法獲取want,并對參數進行解析,實現指定頁面拉起:
import featureAbility from '@ohos.ability.featureAbility';
import router from '@ohos.router';
class PageAbilityStandard {
onCreate() {
featureAbility.getWant().then((want) = > {
if (want.parameters) {
if (want.parameters.page) {
router.pushUrl({ url: want.parameters.page as string });
}
}
})
}
}
export default new PageAbilityStandard();
審核編輯 黃宇
-
鴻蒙
+關注
關注
57文章
2310瀏覽量
42747
發布評論請先 登錄
相關推薦
評論