HarmonyOS 应用开发之启动指定页面
创始人
2024-11-13 04:37:37
0

当PageAbility的启动模式设置为单例时(具体设置方法和典型场景示例见 PageAbility的启动模式 ,缺省情况下是单实例模式),若PageAbility已被拉起,再次启动PageAbility会触发onNewWant回调(即非首次拉起)。应用开发者可以通过want传递启动参数,例如开发者希望指定页面启动PageAbility,可以通过want中的parameters参数传递pages信息,具体示例代码如下:

调用方PageAbility的app.ets中或者page中,使用startAbility再次拉起PageAbility,通过want中的uri参数传递页面信息:

import featureAbility from '@ohos.ability.featureAbility'; import Want from '@ohos.app.ability.Want'; import Logger from '../../utils/Logger';  const TAG: string = 'PagePageAbilityFirst'; 

(async (): Promise => {   let wantInfo: Want = {     bundleName: 'com.samples.famodelabilitydevelop',     abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton',     parameters: { page: 'pages/second' }   };   featureAbility.startAbility({ want: wantInfo }).then((data) => {     Logger.debug(TAG, `restartAbility success : ${data}`);   }); })() 

在目标端PageAbility的onNewWant回调中获取包含页面信息的want参数:

// GlobalContext.ts 构造单例对象 export class GlobalContext {   private constructor() {   }    private static instance: GlobalContext;   private _objects = new Map();    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.push({ 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.push实现启动指定页面。

调用方的页面中实现按钮点击触发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 Logger from '../../utils/Logger';  const TAG: string = 'PagePageAbilityFirst';  @Entry @Component struct PagePageAbilityFirst {   build() {     Column() {       Row() {         Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) {           Text($r('app.string.pageAbility_first_button'))             .fontSize(24)             .fontWeight(FontWeight.Bold)             .textAlign(TextAlign.Start)             .margin({ top: 12, bottom: 11, right: 24, left: 24 })         }       }       .width('100%')       .height(56)       .justifyContent(FlexAlign.Start)       .backgroundColor($r('app.color.backGrounding'))        List({ initialIndex: 0 }) {         ...         ListItem() {           Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {             Text($r('app.string.start_standard_first_button'))               .textAlign(TextAlign.Start)               .fontWeight(FontWeight.Medium)               .margin({ top: 17, bottom: 17, left: 12, right: 92 })               .fontSize(16)               .width(232)               .height(22)               .fontColor($r('app.color.text_color'))           }           .onClick(() => {             let want: Want = {               bundleName: 'com.samples.famodelabilitydevelop',               abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard',               parameters: { page: 'pages/first' }             };             featureAbility.startAbility({ want: want }).then((data) => {               Logger.info(TAG, `startAbility finish:${data}`);             }).catch((err: BusinessError) => {               Logger.info(TAG, `startAbility failed errcode:${err.code}`);             })           })         }         .height(56)         .backgroundColor($r('app.color.start_window_background'))         .borderRadius(24)         .margin({ top: 12, right: 12, left: 12 })          ListItem() {           Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {             Text($r('app.string.start_standard_second_button'))               .textAlign(TextAlign.Start)               .fontWeight(FontWeight.Medium)               .margin({ top: 17, bottom: 17, left: 12, right: 92 })               .fontSize(16)               .width(232)               .height(22)               .fontColor($r('app.color.text_color'))           }           .onClick(() => {             let want: Want = {               bundleName: 'com.samples.famodelabilitydevelop',               abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard',               parameters: { page: 'pages/second' }             };             featureAbility.startAbility({ want: want }).then((data) => {               Logger.info(TAG, `startAbility finish:${data}`);             }).catch((err: BusinessError) => {               Logger.info(TAG, `startAbility failed errcode:${err.code}`);             })           })         }         .height(56)         .backgroundColor($r('app.color.start_window_background'))         .borderRadius(24)         .margin({ top: 12, right: 12, left: 12 })         ...       }       .height('100%')       .backgroundColor($r('app.color.backGrounding'))     }     .width('100%')     .margin({ top: 8 })     .backgroundColor($r('app.color.backGrounding'))   } } 

目标端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.push({ url: want.parameters.page as string });         }       }     })   } }  export default new PageAbilityStandard(); 

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

相关内容

热门资讯

牛牛链接房卡那里有/哪里有卖微... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
微信里玩炸金花房卡在哪弄/微信... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
炸金花房卡链接去哪里买/新二号... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
微信链接金花群房卡如何开/炸金... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...
微信金花房间卡在哪买的/新皇豪... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享受...
微信群金花房卡链接如何获得/微... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
拼三张房卡链接在哪买的/冷酷大... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
终于找到“怎样购买微信金花房卡... 新荣耀是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享...
微信玩拼三张怎么买房卡/新皇豪... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
房卡必备教程“可以自己开房炸金... 新西部是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享...
微信炸金花房卡如何购买/人海大... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享...
开牛牛群怎么买房卡/微信牛牛房... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
拼三张房卡如何购买/新乐游大厅... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
微信炸金花房卡在哪里充/微信炸... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡...
创建拼三张链接房间房卡/新世界... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...
终于找到“微信拼三张房卡哪里购... 新老夫子是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
微信牛牛房卡找谁买/新海贝大厅... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:55051770许多玩家在游戏中会购买房卡来享受...
秒懂教程“牛牛在哪里购买房卡/... 新七喜是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享...
微信里面炸金花房卡在哪买/金牛... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:8488009许多玩家在游戏中会购买房卡来享受...
怎么创建拼三张房间卡房卡/新九... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33903369许多玩家在游戏中会购买房卡来享...