可以通过配置Intent的Action导航到目标AbilitySlice。Page间的导航可以使用startAbility()或startAbilityForResult()方法,获得返回结果的回调为onAbilityResult()。在Ability中调用setResult()可以设置返回结果。
页面布局比较简单,就略过了。
具体请看代码:
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//通过ID找到xml中的button控件
Button button = (Button) findComponentById(ResourceTable.Id_main_button);
//设置点击监听事件
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//点击跳转
// presentForResult(new SecondSlice(),new Intent(),110);
Intent intentNew = new Intent();
Operation operation = new Intent.OperationBuilder().withDeviceId("")
.withBundleName(getBundleName())
.withAbilityName(OtherAbility.class)
.build();
intentNew.setOperation(operation);
intentNew.setParam("key","我从mainAbility到otherAbility");
startAbilityForResult(intentNew,112);
}
});
}
这是跳转的代码。
通过onAbilityResult方法接收回调结果
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
if (resultCode != 0 || resultData == null) {
return;
}
LogUtil.info("onAbilityResult requestCode",requestCode+" resultCode = "+resultCode);
String str_text = resultData.getStringParam("key");
text.setText(str_text);
}
这是接收返回结果的代码。
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_other);
Text textShow = (Text) findComponentById(ResourceTable.Id_other_text_show);
textShow.setText(intent.getStringParam("key"));
Button btn_other = (Button) findComponentById(ResourceTable.Id_btn_other_ability);
btn_other.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//结束当前页面
terminate();
}
});
}
@Override
public void onActive() {
super.onActive();
//设置传递内容
Intent intent = new Intent();
intent.setParam("key","我是从otherAbility回到MainAbility");
getAbility().setResult(0,intent);
}
在另一个Page abilitySlice在onActive方法设置回传的内容。
这就是Page间的导航。