回复
AbilitySlice间导航(新手基础文) 原创
陈浩南xxx
发布于 2021-4-23 11:30
浏览
0收藏
官方链接:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-page-switching-0000000000037999
一:同一Page内导航
present(new TargetSlice(),new Intent());
希望 能够获得其返回结果
ASlice跳转BSlice
presentForResult(new TargetSlice(),new Intent(),111);
BSlice返回带结果 代码
findComponentById(ResourceTable.Id_back_result_btn).setClickedListener(component -> {
Intent resultIntent = new Intent();
resultIntent.setParam("result","我的TargetSlice返回的结果");
setResult(resultIntent);
terminate();
});
ASlice结果接收
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
Log.d("", "onResult %d", requestCode);
if (requestCode == 111) {
// Process resultIntent here.
String resultString = resultIntent.getStringParam("result");
Log.d("", "resultString %s", resultString);
text.setText(resultString);
}
}
二,不同Page间导航
Intent newIntent = new Intent();
// 通过Intent中的OperationBuilder类构造operation对象,
// 指定设备标识(空串表示当前设备)、应用包名、Ability名称
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.pvj.aidemo")
.withAbilityName("com.pvj.aidemo.example.pageroute.PageRouteDemoAbility")
// .withAbilityName(PageRouteDemoAbility.class.getCanonicalName())
.build();
// 把operation设置到intent中
newIntent.setOperation(operation);
startAbility(newIntent);
根据Action 跳转 带结构的
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withAction(Intent.ACTION_QUERY_WEATHER)
.build();
intent.setOperation(operation);
startAbilityForResult(intent, REQ_CODE_QUERY_WEATHER);
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
switch (requestCode) {
case REQ_CODE_QUERY_WEATHER:
// Do something with result.
...
return;
default:
...
}
}
1,config配置
{
"module": {
...
"abilities": [
{
...
"skills":[
{
"actions":[
"ability.intent.QUERY_WEATHER"
]
}
]
...
}
]
...
}
...
}
2,在Ability中配置路由以便支持以此action导航到对应的AbilitySlice
@Override
protected void onStart(Intent intent) {
...
addActionRoute(Intent.ACTION_QUERY_WEATHER, DemoSlice.class.getName());
...
}
在Ability中处理请求,并调用setResult()方法暂存返回结果
@Override
protected void onActive() {
...
Intent resultIntent = new Intent();
setResult(0, resultIntent); //0为当前Ability销毁后返回的resultCode。
...
}
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
赞
3
收藏
回复
相关推荐