技术都是相互借鉴的,鸿蒙的Want类似于Android的Intent,本文将介绍Ability(Android是Activity,Ios是ViewController)直接的跳转通过Want来传递参数,一个Ability跳转到另外一个Ability,另外一个Ability操作完成后,关闭返回有时候不需要有返回值,有时候需要返回值。本文两种情况都实验了。本文模拟从AbilityTest.ets跳转到NewsAbility.ets。
AbilityTest.ets代码
import { common, Want } from '@kit.AbilityKit'
import { hilog } from '@kit.PerformanceAnalysisKit';
import { TAG } from '@ohos/hypium/src/main/Constant';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct AbilityTest {
private context = getContext(this) as common.UIAbilityContext;
build() {
Column(){
Text("Ability跳转测试")
Button("跳转到新闻Ability").onClick(()=>{
// context为Ability对象的成员,在非Ability对象内部调用需要
// 将Context对象传递过去
let wantInfo: Want = {
deviceId: '', // deviceId为空表示本设备
bundleName: 'com.figo.testapplication',//app.json5里面查看
moduleName: 'entry', // module.json5里面查看
abilityName: 'NewsAbility',//需要跳转过去的abilityName,module.json5里面查看
parameters: {//传递的参数
// 自定义信息
param1: '来自AbilityTest页面的param1',
param2: 123
},
};
//1.无需获取返回值startAbility
// this.context.startAbility(wantInfo).then(() => {
// let DOMAIN_NUMBER=1000;
// hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
// }).catch((error: BusinessError) => {
// let DOMAIN_NUMBER=9999
// hilog.error(DOMAIN_NUMBER, TAG, 'startAbility failed.');
// });
//2.需要获取下一个ability的返回值startAbilityForResult
let RESULT_CODE=100;//需要和传递的一致
let DOMAIN_NUMBER=1000;
this.context.startAbilityForResult(wantInfo).then((data) => {
if (data?.resultCode === RESULT_CODE) {
// 获取被调用ability,本demo是NewsAbility返回的参数
let paramNew1 = data.want?.parameters?.paramNew1;
let paramNew2 = data.want?.parameters?.paramNew2;
hilog.info(0x0000, 'testTag', '接收到NewsAbility返回值为:paramNew1=%{public}s,paramNew2=%{public}d', paramNew1,paramNew2);
}
hilog.info(DOMAIN_NUMBER, TAG, JSON.stringify(data.resultCode) ?? '');
}).catch((err: BusinessError) => {
hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability for result. Code is ${err.code}, message is ${err.message}`);
});
})
}
}
}
NewsAbility.ets代码
获取AbilityTest.ets传来的参数
export default class NewsAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
// 接收调用方UIAbility传过来的参数
let funcAbilityWant = want;
let param1 = funcAbilityWant?.parameters?.param1;
let param2 = funcAbilityWant?.parameters?.param2;
hilog.info(0x0000, 'testTag', '接收到参数param1=%{public}s,param2=%{public}d', param1,param2);
}
}
返回值给AbilityTest.ets
import { common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { TAG } from '@ohos/hypium/src/main/Constant';
@Entry
@Component
struct NewsPage {
build() {
Column(){
Text("新闻列表页")
Button("返回").onClick(()=>{
let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; // UIAbilityContext
// 1.无需返回值的情况 context为需要停止的UIAbility实例的AbilityContext
// context.terminateSelf((err) => {
// if (err.code) {
// let DOMAIN_NUMBER = 9999;
// hilog.error(DOMAIN_NUMBER, TAG, `Failed to terminate self. Code is ${err.code}, message is ${err.message}`);
// return;
// }
// })
//2.需要返回值的情况
const RESULT_CODE: number = 100;
let abilityResult: common.AbilityResult = {
resultCode: RESULT_CODE,
want: {
bundleName: 'com.figo.testapplication',
moduleName: 'entry', // moduleName非必选
abilityName: 'EntryAbility',
parameters: {
paramNew1: '来自AbilityTest页面',
paramNew2: 1234567890
},
},
};
context.terminateSelfWithResult(abilityResult, (err) => {
if (err.code) {
let DOMAIN_NUMBER=1000;
hilog.error(DOMAIN_NUMBER, TAG, `Failed to terminate self with result. Code is ${err.code}, message is ${err.message}`);
return;
}
});
})
}
}
}