作者:韩茹

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

鸿蒙巴士专栏作家

一、同Page的AbilitySlice之间的跳转

1.1 present

当发动导航的AbilitySlice和导航指标的AbilitySlice处于同一个Page时,能够通过present()办法实现导航。

@Overrideprotected 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()));  

这样咱们就能够点击第一个页面的按钮跳转到第二个页面,点击第二个页面的按钮跳转到第一个页面。

看一下成果:

1.2 presentForResult

如果开发者心愿在用户从导航指标AbilitySlice返回时,可能取得其返回后果,则该当应用presentForResult()实现导航。用户从导航指标AbilitySlice返回时,零碎将回调onResult()来接管和解决返回后果,开发者须要重写该办法。返回后果由导航指标AbilitySlice在其生命周期内通过setResult()进行设置。

@Overrideprotected void onStart(Intent intent) {    ...    Button button = ...;    button.setClickedListener(listener -> presentForResult(new TargetSlice(), new Intent(), 0));    ...}@Overrideprotected 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:        }    }

咱们运行程序:

咱们也能够察看一下打印的信息:

二、不同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"      }

如果所示:

而后咱们在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);        });

运行,点击第三个按钮:

这里咱们要留神,通过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);            }        });

运行:

如果一个想跳转到不同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"            ]          }        ],

而后在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);        });

运行成果:

这里的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"            ]          }        ]

而后在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页面。。回传数据。。。");    }}

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

而后咱们在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();            }        }    }

好了,咱们来残缺运行一下:

在第二个页面,咱们点击按钮,或者间接点击返回键,都能够回到第一个页面,获取到回传到数据。