当前位置:   article > 正文

不同Page间的AbilitySlice导航_ability 导航的方法

ability 导航的方法

可以通过配置Intent的Action导航到目标AbilitySlice。Page间的导航可以使用startAbility()或startAbilityForResult()方法,获得返回结果的回调为onAbilityResult()。在Ability中调用setResult()可以设置返回结果。

页面布局比较简单,就略过了。

具体请看代码:

  1. @Override
  2. public void onStart(Intent intent) {
  3. super.onStart(intent);
  4. super.setUIContent(ResourceTable.Layout_ability_main);
  5. //通过ID找到xml中的button控件
  6. Button button = (Button) findComponentById(ResourceTable.Id_main_button);
  7. //设置点击监听事件
  8. button.setClickedListener(new Component.ClickedListener() {
  9. @Override
  10. public void onClick(Component component) {
  11. //点击跳转
  12. // presentForResult(new SecondSlice(),new Intent(),110);
  13. Intent intentNew = new Intent();
  14. Operation operation = new Intent.OperationBuilder().withDeviceId("")
  15. .withBundleName(getBundleName())
  16. .withAbilityName(OtherAbility.class)
  17. .build();
  18. intentNew.setOperation(operation);
  19. intentNew.setParam("key","我从mainAbility到otherAbility");
  20. startAbilityForResult(intentNew,112);
  21. }
  22. });
  23. }

这是跳转的代码。

通过onAbilityResult方法接收回调结果

  1. @Override
  2. protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
  3. if (resultCode != 0 || resultData == null) {
  4. return;
  5. }
  6. LogUtil.info("onAbilityResult requestCode",requestCode+" resultCode = "+resultCode);
  7. String str_text = resultData.getStringParam("key");
  8. text.setText(str_text);
  9. }

这是接收返回结果的代码。

  1. @Override
  2. public void onStart(Intent intent) {
  3. super.onStart(intent);
  4. super.setUIContent(ResourceTable.Layout_ability_other);
  5. Text textShow = (Text) findComponentById(ResourceTable.Id_other_text_show);
  6. textShow.setText(intent.getStringParam("key"));
  7. Button btn_other = (Button) findComponentById(ResourceTable.Id_btn_other_ability);
  8. btn_other.setClickedListener(new Component.ClickedListener() {
  9. @Override
  10. public void onClick(Component component) {
  11. //结束当前页面
  12. terminate();
  13. }
  14. });
  15. }
  16. @Override
  17. public void onActive() {
  18. super.onActive();
  19. //设置传递内容
  20. Intent intent = new Intent();
  21. intent.setParam("key","我是从otherAbility回到MainAbility");
  22. getAbility().setResult(0,intent);
  23. }

在另一个Page abilitySlice在onActive方法设置回传的内容。

这就是Page间的导航。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号