关于harmonyos:4AbilitySlice的跳转

44次阅读

共计 19964 个字符,预计需要花费 50 分钟才能阅读完成。

作者:韩茹

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

鸿蒙巴士专栏作家

一、同 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()));
  

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

看一下成果:

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:
        }
    }

咱们运行程序:

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

二、不同 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();}
        }
    }

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

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

正文完
 0