使用router事件跳转到指定UIAbility

2024-01-25 12:20 更新

在卡片中使用postCardAction接口的router能力,能够快速拉起卡片提供方应用的指定UIAbility,因此UIAbility较多的应用往往会通过卡片提供不同的跳转按钮,实现一键直达的效果。例如相机卡片,卡片上提供拍照、录像等按钮,点击不同按钮将拉起相机应用的不同UIAbility,从而提升用户的体验。

通常使用按钮控件来实现页面拉起,示例代码如下:

  • 在卡片页面中布局两个按钮,点击其中一个按钮时调用postCardAction向指定UIAbility发送router事件,并在事件内定义需要传递的内容。
    1. @Entry
    2. @Component
    3. struct WidgetCard {
    4. build() {
    5. Column() {
    6. Button('功能A')
    7. .margin('20%')
    8. .onClick(() => {
    9. console.info('Jump to EntryAbility funA');
    10. postCardAction(this, {
    11. 'action': 'router',
    12. 'abilityName': 'EntryAbility', // 只能跳转到当前应用下的UIAbility
    13. 'params': {
    14. 'targetPage': 'funA' // 在EntryAbility中处理这个信息
    15. }
    16. });
    17. })
    18. Button('功能B')
    19. .margin('20%')
    20. .onClick(() => {
    21. console.info('Jump to EntryAbility funB');
    22. postCardAction(this, {
    23. 'action': 'router',
    24. 'abilityName': 'EntryAbility', // 只能跳转到当前应用下的UIAbility
    25. 'params': {
    26. 'targetPage': 'funB' // 在EntryAbility中处理这个信息
    27. }
    28. });
    29. })
    30. }
    31. .width('100%')
    32. .height('100%')
    33. }
    34. }
  • 在UIAbility中接收router事件并获取参数,根据传递的params不同,选择拉起不同的页面。
    1. import UIAbility from '@ohos.app.ability.UIAbility';
    2. import window from '@ohos.window';
    3. let selectPage = "";
    4. let currentWindowStage = null;
    5. export default class CameraAbility extends UIAbility {
    6. // 如果UIAbility第一次启动,在收到Router事件后会触发onCreate生命周期回调
    7. onCreate(want, launchParam) {
    8. // 获取router事件中传递的targetPage参数
    9. console.info("onCreate want:" + JSON.stringify(want));
    10. if (want.parameters.params !== undefined) {
    11. let params = JSON.parse(want.parameters.params);
    12. console.info("onCreate router targetPage:" + params.targetPage);
    13. selectPage = params.targetPage;
    14. }
    15. }
    16. // 如果UIAbility已在后台运行,在收到Router事件后会触发onNewWant生命周期回调
    17. onNewWant(want, launchParam) {
    18. console.info("onNewWant want:" + JSON.stringify(want));
    19. if (want.parameters.params !== undefined) {
    20. let params = JSON.parse(want.parameters.params);
    21. console.info("onNewWant router targetPage:" + params.targetPage);
    22. selectPage = params.targetPage;
    23. }
    24. if (currentWindowStage != null) {
    25. this.onWindowStageCreate(currentWindowStage);
    26. }
    27. }
    28. onWindowStageCreate(windowStage: window.WindowStage) {
    29. let targetPage;
    30. // 根据传递的targetPage不同,选择拉起不同的页面
    31. switch (selectPage) {
    32. case 'funA':
    33. targetPage = 'pages/FunA';
    34. break;
    35. case 'funB':
    36. targetPage = 'pages/FunB';
    37. break;
    38. default:
    39. targetPage = 'pages/Index';
    40. }
    41. if (currentWindowStage === null) {
    42. currentWindowStage = windowStage;
    43. }
    44. windowStage.loadContent(targetPage, (err, data) => {
    45. if (err && err.code) {
    46. console.info('Failed to load the content. Cause: %{public}s', JSON.stringify(err));
    47. return;
    48. }
    49. });
    50. }
    51. };
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号