HarmonyOS中实现页面跳转的方法汇总 原创 精华

starLWW
发布于 2021-5-11 23:13
浏览
3收藏

–1. 不同Slice间跳转,同一个Ability中,优点是方便,高效,缺点是业务逻辑复杂度受限;

button.setClickedListener(
	listener -> present(new SecondAbilitySlice(), new Intent())
);


–2. 使用Intent借助于ElementName,最常用的页面跳转方式,方便传递参数以及实现相对复杂的业务逻辑交互;

ElementName elementName = new ElementName(……);
intent.setElement(elementName);
intent.setParam(……);
startAbility(intent);


–3. 借助于Operation,可实现跨应用页面跳转;

Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
        .withDeviceId("")
        .withBundleName("com.demoapp")
        .withAbilityName("com.demoapp.FooAbility")
        .build();
 intent.setOperation(operation);
startAbility(intent);


–4. Rout路由(JS),调用router.push()接口将uri指定的页面添加到路由栈中,即跳转到uri指定的页面。在调用router方法之前,需要导入router模块。

调用router.push()路由到详情页;调用router.back()回到首页;

// index.js
import router from '@system.router';
export default {
  launch() {
    router.push ({
      uri: 'pages/detail/detail',
    });
  },
}
// detail.js
import router from '@system.router';
export default {
  launch() {
    router.back();
  },
}

–5. 通过迁移实现分布式设备间页面传递(有请求迁移和请求回迁两种操作)

1)需实现IAbilityContinuation接口

2)需要权限

ohos.permission.GET_DISTRIBUTED_DEVICE_INFO:用于允许获取分布式组网内的设备列表和设备信息
ohos.permission.DISTRIBUTED_DATASYNC:用于允许不同设备间的数据交换
ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE:用于允许监听分布式组网内的设备状态变化
ohos.permission.READ_USER_STORAGE:读取存储卡中的内容
ohos.permission.WRITE_USER_STORAGE:修改或删除存储卡中的内容
ohos.permission.GET_BUNDLE_INFO:用于查询其他应用的信息
ohos.permission.servicebus.ACCESS_SERVICE:分布式数据传输的权限
com.huawei.hwddmp.servicebus.BIND_SERVICE:系统应用使用权限

3)需要获取分布式设备ID(NetworkID)

核心服务类:IContinuationRegisterManager

服务类的常用API方法:
          getContinuationRegisterManager();获取服务类的对象
          register();注册服务
          showDeviceList();获取设备列表
          unregister();注销服务

4)请求迁移关键步骤(假定设备A向设备B迁移)

需要迁移的page实现IAbilityContinuation接口
复写onStartContinuation()方法,做迁移前的准备工作
复写onSaveData()方法,保存迁移数据
在设备B上复写onRestoreData()方法,恢复迁移数据
在设备A上复写onCompleteContinuation()方法,做迁移后的收尾工作
调用continueAbility()或continueAbilityReversibly()发起迁移

5)请求回迁需在设备A上调用reverseContinueAbility()请求回迁

以下关键步骤类似4)

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
4
收藏 3
回复
举报
3条回复
按时间正序
/
按时间倒序
Whyalone
Whyalone

就喜欢读这种简单高效的文章!!!期待更多分享

回复
2021-5-11 23:39:04
longlong899
longlong899

好文,感谢分享,又学到了!

回复
2021-5-12 11:08:04
鸿蒙张荣超
鸿蒙张荣超

赞赞赞!

回复
2021-5-12 11:54:03
回复
    相关推荐