AbilitySlice的跳转

作者:韩茹

公司:程序咖(北京)科技有限公司

鸿蒙巴士专栏作家

一、同Page的AbilitySlice之间的跳转

1.1 present

当发起导航的AbilitySlice和导航目标的AbilitySlice处于同一个Page时,可以通过present()方法实现导航。

@Override
protected void onStart(Intent intent) {

    ...
    Button button = ...;
    button.setClickedListener(listener -> present(new TargetSlice(), new Intent()));
    ...

}
复制代码

这里的present()方法:

// 显示另一个AbilitySlice,可以使用Intent对象传递所需的信息。
public final void present(AbilitySlice targetSlice, Intent intent)
复制代码

我们先在layout目录下的ability_main.xml,添加一个按钮:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33AA0000"
    ohos:orientation="vertical">


    <Button
        ohos:id="$+id:btn1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="点击按钮,跳转到第一个页面"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />

</DirectionalLayout>
复制代码

然后我们在layout目录下,再创建一个xml文件,表示要跳转的第二个页面,ability_second.xml,

<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:padding="10vp"
    ohos:background_element="#2200AA00"
    ohos:orientation="vertical">

    <Text
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text="第二个页面"
        ohos:text_alignment="center"
        ohos:text_size="20fp"

        />

    <Button
        ohos:id="$+id:btn2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="点击按钮,跳转到第一个页面"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        />

</DependentLayout>
复制代码

我们在slice包下新建一个AbilitySlice文件:SecondAbilitySlice.java,用于加载ability_second.xml布局。

public class SecondAbilitySlice extends AbilitySlice{
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);

    }
}
复制代码

然后在MyAbilitySlice中,获取Button组件,并添加点击事件。

package com.example.hanruabilityslicejump.slice;

import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 1.present-----------------------------------
        // 获取按钮
        Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
        // 为按钮添加点击事件
        /**
         * present(AbilitySlice targetSlice, Intent intent)
         * 设置要启动的组件,确定其实位置和目标位置,就是说从哪跳到哪。
         */
        btn1.setClickedListener(component -> present(new SecondAbilitySlice(),new Intent()));



    }

}

复制代码

在SecondAbilitySlice.java的onStart()方法中,也添加点击事件:

     Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
     btn2.setClickedListener(component -> present(new MainAbilitySlice(),new Intent()));
  
复制代码

这样我们就可以点击第一个页面的按钮跳转到第二个页面,点击第二个页面的按钮跳转到第一个页面。

看一下效果:

abilitysliceyunxing1

1.2 presentForResult

如果开发者希望在用户从导航目标AbilitySlice返回时,能够获得其返回结果,则应当使用presentForResult()实现导航。用户从导航目标AbilitySlice返回时,系统将回调onResult()来接收和处理返回结果,开发者需要重写该方法。返回结果由导航目标AbilitySlice在其生命周期内通过setResult()进行设置。

@Override
protected void onStart(Intent intent) {

    ...
    Button button = ...;
    button.setClickedListener(listener -> presentForResult(new TargetSlice(), new Intent(), 0));
    ...

}

@Override
protected void onResult(int requestCode, Intent resultIntent) {
    if (requestCode == 0) {
        // Process resultIntent here.
    }
}
复制代码

这里的presentForResult()方法:

// 显示另一个AbilitySlice,并通过调用setResult(ohos.aafwk.content.Intent)返回目标AbilitySlice设置的结果。
/**
 * targetSlice,明确目标AbilitySlice,不能为null。
 * intent,跳转时携带的信息,不能为null。
 * requestCode,自定义请求代码,不能为负数。
 */

public final void presentForResult(AbilitySlice targetSlice, Intent intent, int requestCode)

复制代码

跳转并回传,操作步骤:

  • 1.在A页面,使用presentForResult(AbilitySlice targetSlice, Intent intent, int requestCode),跳转到第二个页面。
  • 2.在B页面,使用setResult(Intent resultData) ,当B页面结束的时候,会回到A页面。
  • 3.在A页面,会执行onResult(int requestCode, Intent resultIntent)。
    • 验证requestCode,是否是发送时的请求码
    • 操作resultIntent获取数据

我们在ability_main.xml中再添加一个按钮:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33AA0000"
    ohos:orientation="vertical">

    ...

    <Button
        ohos:id="$+id:btn2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="跳转并回传数据"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />

</DirectionalLayout>
复制代码

然后在layout目录下新建一个xml文件,present_for_result.xml:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:padding="20vp"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:textmsg"
        ohos:height="200vp"
        ohos:width="match_parent"
        ohos:background_element="#3300ff00"
        ohos:text_size="25fp"
        ohos:text_alignment="center"
        />

    <Button
        ohos:id="$+id:btnforresult"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="将结果返回给上一个页面"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />
</DirectionalLayout>
复制代码

然后在MainAbilitySlice.java中,获取该按钮跳转到第二个页面,当第二个页面销毁的时候,回传数据:

 				// presentForResult-----------------------------------
        Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
        /**
         * 跳转并回传,操作步骤:
         * 1.在A页面,使用presentForResult(AbilitySlice targetSlice, Intent intent, int requestCode),跳转到第二个页面
         * 2.在B页面,使用setResult(Intent resultData) ,当B页面结束的时候,会回到A页面。
         * 3.在A页面,会执行onResult(int requestCode, Intent resultIntent)。
         *  验证requestCode,是否是发送时的请求码
         *  操作resultIntent获取数据
         */
        btn2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 1. 要跳转到第二个页面,并传值
                Intent intent2 = new Intent();
                intent2.setParam("msg","你是小白兔嚒?");
                // 跳转到详情页面,并返回数据
                presentForResult(new PresentForResultAbilitySlice(),intent2,REQUESTCODE);
            }
            }
        });
复制代码

然后在src下新建一个AbilitySlice文件:PresentForResultAbilitySlice.java,

package com.example.hanruabilityslicejump.slice;

import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class PresentForResultAbilitySlice extends AbilitySlice{
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_present_for_result);
        // 接收intent中数据
        String msg = intent.getStringParam("msg");

        // 将数据设置到Text上。
        Text textMsg = (Text) findComponentById(ResourceTable.Id_textmsg);
        textMsg.setText(msg);

        // 第二个页面上,点击按钮返回第一个页面,并回传数据
        Button  btnForResult = (Button) findComponentById(ResourceTable.Id_btnforresult);

        btnForResult.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 点击按钮,回传数据,并销毁当前的AbilitySlice,就会退回到A页面。
                //回传数据
                Intent intent1 = new Intent();
                intent1.setParam("backMsg","我是长颈鹿");
                setResult(intent1); // 返回A页面,
                System.out.println("B页面。。回传数据。。。");
                terminate();// 销毁当前的AbilitySlice
//                present(new MainAbilitySlice(),intent1);

            }
        });
    }
}

复制代码

这里我们的思路是,先获取上一个页面传来的数据,点击按钮,回传数据到第一个页面。

然后我们在第一个页面中重写onResult()方法,处理回传来的数据。

		// 通过presentForResult()跳转到另一个页面,并通过调用 setResult(ohos.aafwk.content.Intent) 返回目标AbilitySlice设置的结果。
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        System.out.println("requestCode-->"+requestCode);
        System.out.println("-->"+resultIntent);
        switch (requestCode) {
            case REQUESTCODE:
                if(resultIntent != null){
                    String backMsg = resultIntent.getStringParam("backMsg");
                    System.out.println("backMsg-->"+backMsg);
                    new ToastDialog(getContext()).setText(backMsg+"").show();
                }
                break;
            default:
        }
    }
复制代码

我们运行程序:

abilitysliceyunxing2

我们也可以观察一下打印的信息:

WX20210624-161839@2x

二、不同Page的AbilitySlice之间的跳转

AbilitySlice作为Page的内部单元,以Action的形式对外暴露,因此可以通过配置Intent的Action导航到目标AbilitySlice。不同Page之间的导航,不能使用present()或者presentForResult()。可以使用startAbility()或startAbilityForResult()方法,获得返回结果的回调为onAbilityResult()。在Ability中调用setResult()可以设置返回结果。

2.1 startAbility

方式一:根据Ability的全称启动应用。

通过withAbilityName()和withBundleName()来指定要跳转的Ability。

首先我们先新建一个Ability,OtherAbility.java

package com.example.hanruabilityslicejump;

import com.example.hanruabilityslicejump.slice.ThirdAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

public class OtherAbility extends Ability{
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        // 设置另一个Ability,加载的AbilitySlice。
        super.setMainRoute(ThirdAbilitySlice.class.getName());
    }
}

复制代码

设置要加载的主路由是ThirdAbilitySlice,那么我们得在slice目录下新建一个AbilitySlice,ThirdAbilitySlice.java:

package com.example.hanruabilityslicejump.slice;

import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

public class ThirdAbilitySlice extends AbilitySlice{
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_otherability_third);
    }
}

复制代码

这里加载的xml布局文件是otherability_third,所以我们在layout目录下新建一个布局文件:otherability_third.xml:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33aa00aa"
    ohos:orientation="vertical">

    <Text
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:text="我是OtherAbility中的AbilitySlice"
        ohos:text_size="24fp"
        ohos:text_alignment="center"
        />

</DirectionalLayout>
复制代码

最后不要忘记在config.json文件中这册这个OtherAbility:这里我们指定一个action。

 		{
        "skills": [
          {
            "actions": [
              "action.other.show"
            ]
          }
        ],
        "orientation": "unspecified",
        "name": "com.example.hanruabilityslicejump.OtherAbility",
        "type": "page",
        "launchType": "standard"
      }
复制代码

如果所示:

WX20210625-144905@2x

然后我们在ability_main.xml中,添加第三个按钮:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33AA0000"
    ohos:orientation="vertical">

		...
    <Button
        ohos:id="$+id:btn3"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="不同page之间AbilitySlice的跳转"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />


</DirectionalLayout>
复制代码

在MainAbility.java中,添加第三个按钮的点击事件:

				// 3.startAbility-----------------------------------
        Button btn3 = (Button) findComponentById(ResourceTable.Id_btn3);
        btn3.setClickedListener(component -> {
            System.out.println("----btn3点击------");
            //不同Page之间的导航,不能使用present()或者presentForResult()
            Intent intent3 = new Intent();
            // 通过withAbilityName()指定要跳转到Ability,但是需要同时使用withBundleName()。
            Operation operation = new Intent.OperationBuilder()
                    .withAbilityName(OtherAbility.class)
                    .withBundleName("com.example.hanruabilityslicejump")
                    .build();
            intent3.setOperation(operation);
            startAbility(intent3);
        });
复制代码

运行,点击第三个按钮:

abilitysliceyunxing3

这里我们要注意,通过withAbilityName()指定要跳转到Ability,但是需要同时使用withBundleName()。

方式二:也可以通过Action来指定

我们在ability_main.xml中,添加第4个个按钮:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33AA0000"
    ohos:orientation="vertical">



		...
    <Button
        ohos:id="$+id:btn4"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="不同page之间AbilitySlice的跳转2"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />


</DirectionalLayout>
复制代码

然后在MainAbility.java中添加第4个按钮的点击事件,

// 4.startAbility-----------------------------------
        Button btn4 = (Button)findComponentById(ResourceTable.Id_btn4);
        btn4.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                System.out.println("点击btn4。。。");
                Intent intent4 = new Intent();
                // 通过指定Action。
                //setAction()方法过时了。
                //in.setAction("action.other.show");//指定另一个Page中的AbilitySlice的action值
                Operation operation = new Intent.OperationBuilder()
                        .withAction("action.other.show")
                        .build();
                intent4.setOperation(operation);
                startAbility(intent4);
            }
        });
复制代码

运行:

abilitysliceyunxing4

如果一个想跳转到不同Page里的另一个AbilitySlice,可以如下操作。

首先先创建一个xml布局文件,otherability_four.xml:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33aaaa00"
    ohos:orientation="vertical">

    <Text
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:text="我是OtherAbility中的另一个AbilitySlice"
        ohos:text_size="24fp"
        ohos:multiple_lines="true"
        ohos:text_alignment="center"
        />

</DirectionalLayout>
复制代码

然后新建一个AbilitySlice:FourAbilitySlice.java,来指定要加载这个xml文件:

package com.example.hanruabilityslicejump.slice;

import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

public class FourAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_otherability_four);
    }
}

复制代码

在Ability中配置路由以便支持以此action导航到对应的AbilitySlice。

package com.example.hanruabilityslicejump;

import com.example.hanruabilityslicejump.slice.FourAbilitySlice;
import com.example.hanruabilityslicejump.slice.StartAbilityForResultAbilitySlice;
import com.example.hanruabilityslicejump.slice.ThirdAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

public class OtherAbility extends Ability{
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        // set the main route,默认加载的AbilitySlice。
        super.setMainRoute(ThirdAbilitySlice.class.getName());

        // set the action route
        super.addActionRoute("action.other.four", FourAbilitySlice.class.getName());
        
    }
}

复制代码

这里的action:”action.other.four”,也需要在config.json中进行配置:

			"skills": [
          {
            "actions": [
              "action.other.show",
              "action.other.four"
            ]
          }
        ],
复制代码

WX20210625-145249@2x

然后在ability_main.xml中再添加一个按钮:第5个

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33AA0000"
    ohos:orientation="vertical">


		...
    <Button
        ohos:id="$+id:btn5"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="不同page之间另一个AbilitySlice"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />



</DirectionalLayout>
复制代码

如果我们想通过点击第5个按钮,来打开OtherAbility中的FourAbilitySlice:

 				// 5.startAbility-----------------------------------
        Button btn5 = (Button)findComponentById(ResourceTable.Id_btn5);
        btn5.setClickedListener(component->{
                System.out.println("点击btn5。。。");
                Intent intent5 = new Intent();
                // 通过指定Action。
                Operation operation = new Intent.OperationBuilder()
                        .withAction("action.other.four")
                        .build();
                intent5.setOperation(operation);
                startAbility(intent5);
        });
复制代码

运行效果:

abilitysliceyunxing5

这里的Action,还可以使用一些系统的,比如打开拨号等。我们可以在Intent章节详细介绍。

2.2 startAbilityForResult

先说一下思路:

1、首先要在第一个Ability的AbilitySlice中,构造Intent以及包含Action的Operation对象,并调用startAbilityForResult()方法发起请求。

2、根据startAbilityForResult()中的参数,跳转到指定的另一个Ability的AbilitySlice中。

3、在另一个Ability中处理请求,并调用setResult()方法暂存返回结果。

4、回到第一个Ability,重写onAbilityResult(),进行处理回传的结果。

我们通过代码来实现一下,首先在layout目录下新建一个xml文件:start_ability_for_result.xml,用作要跳转到的布局页面:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:padding="20vp"
    ohos:orientation="vertical">

    <Text
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="25fp"
        ohos:text="不同的PageAbility"
        ohos:text_alignment="center"
        ohos:bottom_margin="30vp"
        />

    <Text
        ohos:id="$+id:textmsg"
        ohos:height="200vp"
        ohos:width="match_parent"
        ohos:background_element="#3300ffff"
        ohos:text_size="25fp"
        ohos:text_alignment="center"
        />

    <Button
        ohos:id="$+id:btnforresult2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="将结果返回给上一个页面"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />
</DirectionalLayout>
复制代码

然后在slice下新建一个AbilitySlice,StartAbilityForResultAbilitySlice.java,用于要跳转到的界面,首先先加载一个布局,就是刚刚上面创建的xml。

public class StartAbilityForResultAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_start_ability_for_result);
    }
复制代码

我们需要在OtherAbility中设置action:

public class OtherAbility extends Ability{
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        // set the main route,默认加载的AbilitySlice。
        super.setMainRoute(ThirdAbilitySlice.class.getName());

        // set the action route
        super.addActionRoute("action.other.four", FourAbilitySlice.class.getName());
        super.addActionRoute("action.other.result", StartAbilityForResultAbilitySlice.class.getName());
    }
复制代码

并且在config.json中注册action:

 "skills": [
          {
            "actions": [
              "action.other.show",
              "action.other.four",
              "action.other.result"
            ]
          }
        ]
复制代码

WX20210625-145420@2x

然后在ability_main.xml中,再添加一个按钮:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:background_element="#33AA0000"
    ohos:orientation="vertical">


		...

    <Button
        ohos:id="$+id:btn6"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="不同page之间AbilitySlice的跳转并回传数据"
        ohos:center_in_parent="true"
        ohos:text_size="20fp"
        ohos:multiple_lines="true"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        ohos:top_margin="20vp"
        />


</DirectionalLayout>
复制代码

在MainAbility中添加按钮6的点击事件,指定好action:

				// 6.startAbilityForResult-----------------------------------

        Button btn6 = (Button)findComponentById(ResourceTable.Id_btn6);
        btn6.setClickedListener(component-> {
                System.out.println("点击btn6。。。");
                Intent intent6 = new Intent();
                intent6.setParam("message","面朝大海,春暖花开");
                Operation operation = new Intent.OperationBuilder()
                        .withAction("action.other.result") // 指定Action
                        .build();
                intent6.setOperation(operation);
                startAbilityForResult(intent6,REQUESTCODEFORRESULT);

        });
复制代码

我们需要在StartAbilityForResultAbilitySlice.java中,进行处理发送来的数据:

package com.example.hanruabilityslicejump.slice;

import com.example.hanruabilityslicejump.MainAbility;
import com.example.hanruabilityslicejump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class StartAbilityForResultAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_start_ability_for_result);

        // 接收intent中数据
        String msg = intent.getStringParam("message");

        // 将数据设置到Text上。
        Text textMsg = (Text) findComponentById(ResourceTable.Id_textmsg);
        textMsg.setText(msg);

        // 第二个页面上,点击按钮返回第一个页面,并回传数据
        Button btnForResult2 = (Button) findComponentById(ResourceTable.Id_btnforresult2);

        btnForResult2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // 毁当前的AbilitySlice,就会退回到A页面。

                terminate();// 销毁当前的AbilitySlice

            }
        });


    }
}

复制代码

这里我们将上一个页面传来的数据,显示到Text上,按钮的点击事件中,我们只是调用terminate(),销毁当前的AbilitySlice,那么就会退回到上一个页面。

这里要注意,给MainAbilitySlice回传的数据,要写到OtherAbility的onActive()中:

package com.example.hanruabilityslicejump;

import com.example.hanruabilityslicejump.slice.FourAbilitySlice;
import com.example.hanruabilityslicejump.slice.StartAbilityForResultAbilitySlice;
import com.example.hanruabilityslicejump.slice.ThirdAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

public class OtherAbility extends Ability{
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        // set the main route,默认加载的AbilitySlice。
        super.setMainRoute(ThirdAbilitySlice.class.getName());

        // set the action route
        super.addActionRoute("action.other.four", FourAbilitySlice.class.getName());
        super.addActionRoute("action.other.result", StartAbilityForResultAbilitySlice.class.getName());
    }

    @Override
    protected void onActive() {
        super.onActive();
        System.out.println("======OtherAbility=======onActive()");
        Intent intent1 = new Intent();
        intent1.setParam("backMessage","星辰大海");
        setResult(0,intent1); //0为当前Ability销毁后返回的resultCode。

        System.out.println("B页面。。回传数据。。。");

    }
}


复制代码

当点击按钮跳转过来后,我们看一下打印的结果:

WX20210625-110612@2x

然后我们在onAbilityResult()处理结果:

// 处理startAbilityForResult()回传的结果
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        System.out.println("requestCode:" + requestCode + ", resultCode:-->" + resultCode);
        if (requestCode == REQUESTCODEFORRESULT && resultCode == 0) {
            if (resultData != null) {
                String backMessage = resultData.getStringParam("backMessage");
                System.out.println("backMessage-->" + backMessage);
                new ToastDialog(getContext()).setText(backMessage + "").show();
            }else{
                new ToastDialog(getContext()).setText("没有获取到回传到数据。。").show();
            }
        }
    }
复制代码

好了,我们来完整运行一下:

abilitysliceyunxing6

在第二个页面,我们点击按钮,或者直接点击返回键,都可以回到第一个页面,获取到回传到数据。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享