HarmonyOS鸿蒙Next中Slice之间导航从目标Slice返回到前一个Slice的时候传参问题
HarmonyOS鸿蒙Next中Slice之间导航从目标Slice返回到前一个Slice的时候传参问题 按照官方文档中测试从目标Slice返回时回传参数发现回传的intent对象是空的问题。
在AbilitySlice导航这章中介绍到: 如果开发者希望在用户从导航目标AbilitySlice返回时,能够获得其返回结果,则应当使用presentForResult()实现导航。用户从导航目标AbilitySlice返回时,系统将回调onResult()来接收和处理返回结果,开发者需要重写该方法。返回结果由导航目标AbilitySlice在其生命周期内通过setResult()进行设置。
但是按照官方的代码测试在返回到前一个Slice的时候onResult接受到的intent为空,而且在按照setResult的时候官方文档给的代码好像是有问题。
官方代码:
@Override
protected void onActive() {
...
Intent resultIntent = new Intent();
setResult(0, resultIntent);
...
}
如果按照官方给定的这个setResult是会报错的,说没有这个setResult的方法,因为在实际写的代码中只可以传入一个参数就是intent的参数。
我的代码:
前一个Slice:
com.example.myapplication.slice;
com.example.myapplication.ResourceTable;
ohos.aafwk.ability.AbilitySlice;
ohos.aafwk.content.Intent;
ohos.aafwk.content.IntentParams;
ohos.agp.components.Button;
ohos.agp.components.Component;
ohos.agp.components.DirectionalLayout;
ohos.agp.components.DirectionalLayout.LayoutConfig;
ohos.agp.components.Text;
ohos.agp.colors.RgbColor;
ohos.agp.components.element.ShapeElement;
ohos.agp.utils.Color;
ohos.agp.utils.TextAlignment;
MainAbilitySlice AbilitySlice {
DirectionalLayout = DirectionalLayout();
onStart(Intent intent) {
.onStart(intent);
.setUIContent(ResourceTable.);
Button button = (Button) findComponentById(ResourceTable.);
}
onActive() {
Button button = (Button) findComponentById(ResourceTable.);
(button != ) {
button.setClickedListener(listener->presentForResult(ForthAbilitySlice(),Intent(),));
}
}
onForeground(Intent intent) {
.onForeground(intent);
}
onResult(requestCode, Intent resultIntent) {
(requestCode==){
IntentParams myname = resultIntent.getParam();
System..println(+myname);
}
}
}
目标Silce:
com.example.myapplication.slice;
com.example.myapplication.ResourceTable;
ohos.aafwk.ability.AbilitySlice;
ohos.aafwk.content.Intent;
ohos.agp.components.DirectionalLayout;
ForthAbilitySlice AbilitySlice {
DirectionalLayout = DirectionalLayout();
onStart(Intent intent) {
.onStart(intent);
.setUIContent(ResourceTable.);
}
onActive() {
System..println();
Intent resultIntent = Intent();
resultIntent.setParam(,);
setResult(resultIntent);
}
onBackPressed() {
setResult(Intent().setParam(,));
.onBackPressed();
}
onForeground(Intent intent) {
.onForeground(intent);
}
}
日志中也可以看到可以跳转到目标Slice
在返回前一个Slice的时候的日志:
更多关于HarmonyOS鸿蒙Next中Slice之间导航从目标Slice返回到前一个Slice的时候传参问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
我发现错误在哪儿了,原来是我的获取参数的方法用错了,应该用getParamString,而不是getParam!
更多关于HarmonyOS鸿蒙Next中Slice之间导航从目标Slice返回到前一个Slice的时候传参问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
亲什么时候给答复,感觉每次稍后答复就像石城大海的样子!
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17
亲什么时候给答复,感觉每次稍后答复就像石城大海的样子!
在HarmonyOS鸿蒙Next中,Slice之间的导航可以通过present
和dismiss
方法实现。当从目标Slice返回到前一个Slice时,可以通过dismiss
方法的result
参数传递数据。目标Slice在调用dismiss
时,可以将需要传递的参数封装到Intent
对象中,并通过setResult
方法设置返回结果。前一个Slice可以通过重写onResult
方法来接收传递的参数。具体实现如下:
- 在目标Slice中,调用
dismiss
方法时传递参数:
let intent = new Intent();
intent.setParam("key", "value");
this.setResult(intent);
this.dismiss();
- 在前一个Slice中,重写
onResult
方法接收参数:
onResult(intent: Intent) {
let value = intent.getParam("key");
// 处理接收到的参数
}
通过这种方式,可以在Slice之间传递参数。
在HarmonyOS鸿蒙Next中,从目标Slice返回到前一个Slice时,可以通过setResult()
方法传递参数。在目标Slice中使用setResult()
设置结果数据,并在前一个Slice中通过onResult()
方法接收。具体步骤:
- 目标Slice:使用
setResult(int resultCode, Intent resultData)
设置返回的数据。 - 前一个Slice:重写
onResult(int requestCode, int resultCode, Intent resultData)
方法,接收并处理返回的数据。
注意:setResult()
和onResult()
需要与startAbilityForResult()
配合使用,确保前一个Slice能正确接收返回的数据。