AbilitySlice 间跳转
present
当发起的 AbilitySlice 和目标的 AbilitySlice 处于同一个 Page 时,您可以通过 present()方法实现跳转
界面一如图:点击跳转界面二并传递参数,AbilitySlice之前传参也通过Intent,如下代码所示:
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initView();
}
private void initView() {
Text text= (Text) findComponentById(ResourceTable.Id_text_helloworld);
text.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
AbilitySlice abilitySlice= new MainAbility2Slice();
Intent intent=new Intent();
intent.setParam("data","我是数据");
present(abilitySlice,intent);
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
界面二,通过Intent接收参数,如下代码所示
public class MainAbility2Slice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main2);
String data = intent.getStringParam("data");
Text text = (Text) findComponentById(ResourceTable.Id_text_data);
text.setText(data);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
presentForResult
如果希望从目标 AbilitySlice 返回时,能够获得其返回结果,则应当使用 presentForResult()实现跳转。 onResult() 来接收和处理返回结果,返回结果由导航目标 AbilitySlice 在其生命周期内通过 setResult()进行设置。
示例:
1.AbilitySlice1界面,点击界面二,使用presentForResult跳转并带参数“我是数据”
2.AbilitySlice2界面,获取“我是数据”,点击返回界面一,调用setResult,传参数“返回参数”
3.AbilitySlice1界面,重写onResult方法,获取返回参数
如图所示:
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbilitySlice extends AbilitySlice {
private Text result;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initView();
}
private void initView() {
Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
result = (Text) findComponentById(ResourceTable.Id_text_result);
text.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
AbilitySlice abilitySlice = new MainAbility2Slice();
Intent intent = new Intent();
intent.setParam("data", "我是数据");
presentForResult(abilitySlice, intent, 100);
}
});
}
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
if (requestCode == 100) {
if (resultIntent == null) {
return;
}
String back_data = resultIntent.getStringParam("back_data");
result.setText(back_data);
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbility2Slice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main2);
Text text = (Text) findComponentById(ResourceTable.Id_text_data);
Text text_finish = (Text) findComponentById(ResourceTable.Id_text_finish);
String data = intent.getStringParam("data");
text.setText(data);
text_finish.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent1 = new Intent();
intent1.setParam("back_data","返回数据");
setResult(intent1);
//销毁
terminate();
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
备注:系统为每个 Page 维护了一个 AbilitySlice 实例的栈,每个进入前台的 AbilitySlice 实例均会入栈。当在调用 present()或 presentForResult()时指定的 AbilitySlice 实例已经在栈中存在时,则栈中位于此实例之上的 AbilitySlice 均会出栈并终止其生命周期。
不同 Page 间跳转
不同 Page 中的 AbilitySlice 相互不可见,通过配置 Intent 的 Action 导航到目标 AbilitySlice。Page 间的导航可以 使用 startAbility()或 startAbilityForResult()方法,获得返回结果的回调为 onAbilityResult(),。在 Ability 中调用 setResult()可以设置返回结果
startAbility
startAbility有两种方式。
- 指定withAbilityName和withBundleName,
withAbilityName为目标的Ability,withBundleName为目标Ability的包名。 - 指定withAction,在config.json中注册skills,为ability命名action,
action的值为withAction。
通过withAbilityName和withBundleName跳转
如代码所示,点击按钮“不同page跳转main2”,跳转MainAbility2,MainAbility2通过intent获取参数“我是数据”并显示。
//不同page跳转
text_other_page.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setParam("data", "我是数据");
Operation operation = new Intent.OperationBuilder()
.withAbilityName(MainAbility2.class)
.withBundleName("com.example.myapplication")
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
通过withAction跳转
与withAbilityName和withBundleName类似,但须先在config文件里注册,如代码所示
actions为action.main.second。withAction="action.main.second"即可跳转到MainAbility2
{
"skills": [
{
"actions": [
"action.main.second",
]
}
],
"orientation": "unspecified",
"name": "com.example.myapplication.MainAbility2",
"icon": "$media:icon",
"description": "$string:mainability2_description",
"label": "$string:entry_MainAbility2",
"type": "page",
"launchType": "standard"
}
//不同page跳转
text_other_page.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setParam("data", "我是数据");
// Operation operation = new Intent.OperationBuilder()
// .withAbilityName(MainAbility2.class)
// .withBundleName("com.example.myapplication")
// .build();
Operation operation = new Intent.OperationBuilder()
.withAction("action.main.second")
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
startAbility 跳转到不同的slice(相当于Android中跳转到不同的fragment)
如果你还是想跳转到MainAbility2,但显示不是同一个slice,可按如下步骤执行:
- 先定义一个不同的slice-MainAbility3Slice
- 在config.json里面注册action,这边定义为action.main.three
- 在MainAbility2里面为MainAbility3Slice添加路由,super.addActionRoute
- 点击“不同page跳转不同slice”按钮,跳转MainAbility2,withAction指定为"action.main.three",跳转到MainAbility3Slice界面
代码如下:
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbility3Slice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main3);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
{
"skills": [
{
"actions": [
"action.main.second",
"action.main.three"
]
}
],
"orientation": "unspecified",
"name": "com.example.myapplication.MainAbility2",
...
}
package com.example.myapplication;
import com.example.myapplication.slice.MainAbility2Slice;
import com.example.myapplication.slice.MainAbility3Slice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility2 extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbility2Slice.class.getName());
super.addActionRoute("action.main.three", MainAbility3Slice.class.getName());
}
@Override
protected void onActive() {
super.onActive();
Intent intent1 = new Intent();
intent1.setParam("back_data","返回数据");
setResult(0,intent1);
}
}
//点击跳转到
text_other_page_slice.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setParam("data", "我是数据");
Operation operation = new Intent.OperationBuilder()
.withAction("action.main.three")
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
startAbilityForResult
回忆上面讲的内容,slice间返参数用presentForResult,重写onResult,
ability间互传参数使用startAbilityForResult,重写方法onAbilityResult方法。在ability2重写onActive,返回参数(对比Android,startActivityForResult,onActivityResult)
ability1跳转到MainAbility2,传参“page 传参数,等待返回”,MainAbility2接收后返回并传参数“返回数据”
代码如下
ability1
private void startPageAbility() {
text_other_page_result.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setParam("data", "page 传参数,等待返回");
Operation operation = new Intent.OperationBuilder()
.withAction("action.main.second")
.build();
intent.setOperation(operation);
startAbilityForResult(intent, 100);
}
});
}
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
super.onAbilityResult(requestCode, resultCode, resultData);
if (requestCode == 100) {
if (resultData == null) {
return;
}
String back_data = resultData.getStringParam("back_data");
result.setText("ability:" + back_data);
}
}
MainAbility2
package com.example.myapplication;
import com.example.myapplication.slice.MainAbility2Slice;
import com.example.myapplication.slice.MainAbility3Slice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility2 extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbility2Slice.class.getName());
super.addActionRoute("action.main.three", MainAbility3Slice.class.getName());
}
@Override
protected void onActive() {
super.onActive();
Intent intent1 = new Intent();
intent1.setParam("back_data","返回数据");
setResult(0,intent1);
}
}
完整代码如下:
package com.example.myapplication.slice;
import com.example.myapplication.MainAbility2;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbilitySlice extends AbilitySlice {
private Text result;
private Text text_other_page;
private Text text_other_page_result;
private Text text_other_page_slice;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initView();
startPageAbility();
}
private void startPageAbility() {
//不同page跳转
text_other_page.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setParam("data", "我是数据");
// 通过withAbilityName()指定要跳转到Ability,但是需要同时使用withBundleName()。
// Operation operation = new Intent.OperationBuilder()
// .withAbilityName(MainAbility2.class)
// .withBundleName("com.example.myapplication")
// .build();
Operation operation = new Intent.OperationBuilder()
.withAction("action.main.second")
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
text_other_page_result.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setParam("data", "page 传参数,等待返回");
Operation operation = new Intent.OperationBuilder()
.withAction("action.main.second")
.build();
intent.setOperation(operation);
startAbilityForResult(intent, 100);
}
});
text_other_page_slice.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent = new Intent();
intent.setParam("data", "我是数据");
Operation operation = new Intent.OperationBuilder()
.withAction("action.main.three")
.build();
intent.setOperation(operation);
startAbility(intent);
}
});
}
private void initView() {
Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
result = (Text) findComponentById(ResourceTable.Id_text_result);
text_other_page = (Text) findComponentById(ResourceTable.Id_text_other_page);
text_other_page_result = (Text) findComponentById(ResourceTable.Id_text_other_page_result);
text_other_page_slice = (Text) findComponentById(ResourceTable.Id_text_other_page_slice);
text.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
AbilitySlice abilitySlice = new MainAbility2Slice();
Intent intent = new Intent();
intent.setParam("data", "我是数据");
presentForResult(abilitySlice, intent, 100);
}
});
}
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
super.onAbilityResult(requestCode, resultCode, resultData);
if (requestCode == 100) {
if (resultData == null) {
return;
}
String back_data = resultData.getStringParam("back_data");
result.setText("ability:" + back_data);
}
}
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
if (requestCode == 100) {
if (resultIntent == null) {
return;
}
String back_data = resultIntent.getStringParam("back_data");
result.setText(back_data);
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbility2Slice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main2);
Text text = (Text) findComponentById(ResourceTable.Id_text_data);
Text text_finish = (Text) findComponentById(ResourceTable.Id_text_finish);
String data = intent.getStringParam("data");
text.setText(data);
text_finish.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//销毁
terminate();
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
package com.example.myapplication;
import com.example.myapplication.slice.MainAbility2Slice;
import com.example.myapplication.slice.MainAbility3Slice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility2 extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbility2Slice.class.getName());
super.addActionRoute("action.main.three", MainAbility3Slice.class.getName());
}
@Override
protected void onActive() {
super.onActive();
Intent intent1 = new Intent();
intent1.setParam("back_data","返回数据");
setResult(0,intent1);
}
}
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbilitySlice extends AbilitySlice {
private Text result;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initView();
}
private void initView() {
Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
result = (Text) findComponentById(ResourceTable.Id_text_result);
text.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
AbilitySlice abilitySlice = new MainAbility2Slice();
Intent intent = new Intent();
intent.setParam("data", "我是数据");
presentForResult(abilitySlice, intent, 100);
}
});
}
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
if (requestCode == 100) {
if (resultIntent == null) {
return;
}
String back_data = resultIntent.getStringParam("back_data");
result.setText(back_data);
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
public class MainAbility3Slice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main3);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
{
"skills": [
{
"actions": [
"action.main.second",
"action.main.three"
]
}
],
"orientation": "unspecified",
"name": "com.example.myapplication.MainAbility2",
"icon": "$media:icon",
"description": "$string:mainability2_description",
"label": "$string:entry_MainAbility2",
"type": "page",
"launchType": "standard"
}