【Harmony OS】【JAVA UI】abilitySlice和ability跳转方式

​Ability跳转方式


准备阶段

参考资料 AbilitySlice间导航IntentAbility

新建TwoAbility界面(如下图所示)

cke_1197.png

1、startAbility

mainAbilitySlice代码如下

 Intent intent=new Intent();
                Operation operation=new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(TwoAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);

2、startAbilityForResult

mainAbilitySlice 写startAbilityForResult

 Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(TwoAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                startAbilityForResult(intent, 1001);

在mainAbilitySlice 重写onAbilityResult的方法,代码如下

@Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if (requestCode == 1001 && resultCode == 1002) {
            IntentParams myIntentParams= resultData.getParams();
           String value= (String) myIntentParams.getParam("key");
            System.err.println("#####" + value);
        }
    }

在TwoAbilitySlice调用setResult的方法

       Intent intent=new Intent();
                intent.setParam("key","value");
                getAbility().setResult(1002,intent);
                terminateAbility();//todo 关闭该Ability

AbilitySlice跳转方式

同一Page内导航

1、present

在mainAbilitySlice写代码如下

  present(new TwoAbilitySlice(),new Intent());

2、presentForResult

在mainability使用presentForResult的方法,代码如下

  presentForResult(new TwoAbilitySlice(),new Intent(),1003);

在mainabilitySlice重写onResult的方法,代码如下

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        if (requestCode == 1003) {
            IntentParams intentParams=  resultIntent.getParams();
            String value= (String) intentParams.getParam("key");
            System.err.println("#####"+value);
        }
    }

在TwoabilitySlice实现点击并关闭该界面

  Intent intent=new Intent();
                intent.setParam("key","value");
               setResult(intent);
                terminate();//todo 关闭该AbilitySlice

 

不同Page间导航

1、准备工作

在MainAbility的onStart方法中调用addActionRoute的方法,代码如下

 @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        addActionRoute("com.pay", TwoAbilitySlice.class.getName());
    }

2、在mainAbilitySlice调用 startAbility的方法,代码如下

  Intent intent=new Intent();
                Operation operation=new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAction("com.pay")//todo action 要和TwoAbilitySlice的action相同
                        .withAbilityName(MainAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

posted @ 2022-08-09 07:43  华为开发者论坛  阅读(141)  评论(0编辑  收藏  举报