调用方使用startAbilityForResult时,被调用方如何返回数据?

edice 显示全部楼层 发表于 2023-9-26 09:50:01

【问题描述】

在一个EntryAbility启动另外一个EntryAbility,调用方使用startAbilityForResult时,被调用方如何返回数据?

【运行环境】

硬件:rk3568; ROM: 3.2 Beta5; API 9

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

精彩评论1

HmD

沙发 发表于 2023-9-27 09:34:56

参考链接:启动应用内的UIAbility并获取返回结果

参考以下示例:在应用中将入口功能和帐号登录功能分别设计为两个独立的UIAbility,在帐号登录UIAbility中完成登录操作后,需要将登录的结果返回给入口UIAbility。

1、在EntryAbility中,调用startAbilityForResult()接口启动FuncAbility,异步回调中的data用于接收FuncAbility停止自身后返回给EntryAbility的信息。示例中的context的获取方式请参见获取UIAbility的上下文信息

import common from '@ohos.app.ability.common';

import Want from '@ohos.app.ability.Want';

import { BusinessError } from '@ohos.base';


let context: common.UIAbilityContext = ...; // UIAbilityContext

let want: Want = {

  deviceId: '', // deviceId为空表示本设备

  bundleName: 'com.example.myapplication',

  moduleName: 'func', // moduleName非必选

  abilityName: 'FuncAbility',

  parameters: { // 自定义信息

    info: '来自EntryAbility Index页面',

  },

}

// context为调用方UIAbility的UIAbilityContext

context.startAbilityForResult(want).then((data) => {

  // ...

}).catch((err: BusinessError) => {

  console.error(\`Failed to start ability for result. Code is \${err.code}, message is \${err.message}\`);

})

2、在FuncAbility停止自身时,需要调用terminateSelfWithResult()方法,入参abilityResult为FuncAbility需要返回给EntryAbility的信息。

import common from '@ohos.app.ability.common';

import Want from '@ohos.app.ability.Want';


let context: common.UIAbilityContext = ...; // UIAbilityContext

const RESULT\_CODE: number = 1001;

let abilityResult: common.AbilityResult = {

  resultCode: RESULT\_CODE,

  want: {

    bundleName: 'com.example.myapplication',

    moduleName: 'func', // moduleName非必选

    abilityName: 'FuncAbility',

    parameters: {

      info: '来自FuncAbility Index页面',

    },

  },

}

// context为被调用方UIAbility的AbilityContext

context.terminateSelfWithResult(abilityResult, (err) => {

  if (err.code) {

    console.error(\`Failed to terminate self with result. Code is \${err.code}, message is \${err.message}\`);

    return;

  }

});

3、FuncAbility停止自身后,EntryAbility通过startAbilityForResult()方法回调接收被FuncAbility返回的信息,RESULT_CODE需要与前面的数值保持一致。

import common from '@ohos.app.ability.common';

import Want from '@ohos.app.ability.Want';

import { BusinessError } from '@ohos.base';


let context: common.UIAbilityContext = ...; // UIAbilityContext

const RESULT\_CODE: number = 1001;


let want: Want = {

  deviceId: '', // deviceId为空表示本设备

  bundleName: 'com.example.myapplication',

  moduleName: 'func', // moduleName非必选

  abilityName: 'FuncAbility',

}


// context为调用方UIAbility的UIAbilityContext

context.startAbilityForResult(want).then((data) => {

  if (data?.resultCode === RESULT\_CODE) {

    // 解析被调用方UIAbility返回的信息

    let info = data.want?.parameters?.info;

    // ...

  }

}).catch((err: BusinessError) => {

  console.error(\`Failed to start ability for result. Code is \${err.code}, message is \${err.message}\`);

})

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部