龙空技术网

AbilitySlice的跳转

RubyHan 29

前言:

现在同学们对“java点击按钮跳转界面”都比较讲究,同学们都想要知道一些“java点击按钮跳转界面”的相关文章。那么小编在网上收集了一些关于“java点击按钮跳转界面””的相关内容,希望姐妹们能喜欢,我们快快来了解一下吧!

作者:韩茹

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

鸿蒙巴士专栏作家

一、同Page的AbilitySlice之间的跳转1.1 present

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

@Overridebrprotected void onStart(Intent intent) {brbr    ...br    Button button = ...;br    button.setClickedListener(listener -> present(new TargetSlice(), new Intent()));br    ...brbr}

这里的present()方法:

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

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

<?xml version="1.0" encoding="utf-8"?>br<DirectionalLayoutbr    xmlns:ohos=";br    ohos:height="match_parent"br    ohos:width="match_parent"br    ohos:background_element="#33AA0000"br    ohos:orientation="vertical">brbrbr    <Buttonbr        ohos:id="$+id:btn1"br        ohos:height="match_content"br        ohos:width="match_content"br        ohos:text="点击按钮,跳转到第一个页面"br        ohos:center_in_parent="true"br        ohos:text_size="20fp"br        ohos:layout_alignment="horizontal_center"br        ohos:background_element="#EEEEEE"br        ohos:padding="10vp"br        ohos:top_margin="20vp"br        />brbr</DirectionalLayout>

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

<?xml version="1.0" encoding="utf-8"?>br<DependentLayoutbr    xmlns:ohos=";br    ohos:height="match_parent"br    ohos:width="match_parent"br    ohos:padding="10vp"br    ohos:background_element="#2200AA00"br    ohos:orientation="vertical">brbr    <Textbr        ohos:height="match_content"br        ohos:width="match_parent"br        ohos:text="第二个页面"br        ohos:text_alignment="center"br        ohos:text_size="20fp"brbr        />brbr    <Buttonbr        ohos:id="$+id:btn2"br        ohos:height="match_content"br        ohos:width="match_content"br        ohos:text="点击按钮,跳转到第一个页面"br        ohos:center_in_parent="true"br        ohos:text_size="20fp"br        ohos:background_element="#EEEEEE"br        ohos:padding="10vp"br        />brbr</DependentLayout>

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

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

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

package com.example.hanruabilityslicejump.slice;brbrimport com.example.hanruabilityslicejump.ResourceTable;brimport ohos.aafwk.ability.AbilitySlice;brimport ohos.aafwk.content.Intent;brimport ohos.agp.components.Button;brbrpublic class MainAbilitySlice extends AbilitySlice {br    @Overridebr    public void onStart(Intent intent) {br        super.onStart(intent);br        super.setUIContent(ResourceTable.Layout_ability_main);brbr        // 1.present-----------------------------------br        // 获取按钮br        Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);br        // 为按钮添加点击事件br        /**br         * present(AbilitySlice targetSlice, Intent intent)br         * 设置要启动的组件,确定其实位置和目标位置,就是说从哪跳到哪。br         */br        btn1.setClickedListener(component -> present(new SecondAbilitySlice(),new Intent()));brbrbrbr    }brbr}br

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

     Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);br     btn2.setClickedListener(component -> present(new MainAbilitySlice(),new Intent()));br  

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

看一下效果:

1.2 presentForResult

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

@Overridebrprotected void onStart(Intent intent) {brbr    ...br    Button button = ...;br    button.setClickedListener(listener -> presentForResult(new TargetSlice(), new Intent(), 0));br    ...brbr}brbr@Overridebrprotected void onResult(int requestCode, Intent resultIntent) {br    if (requestCode == 0) {br        // Process resultIntent here.br    }br}

这里的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=";    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=";    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=";    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=";    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=";    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=";    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=";    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=";    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=";    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();            }        }    }

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

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

标签: #java点击按钮跳转界面 #java如何实现跳转到指定页面