同一个Page中slice之间的跳转
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:main" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main" ohos:layout_alignment="horizontal_center" ohos:text="main" ohos:text_size="40vp" /> </DirectionalLayout>
//默认路由到MainAbilitySlice public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Text text= (Text) findComponentById(ResourceTable.Id_main); text.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { //跳转到目标AbilitySlice,在intent1携带数据,并指定请求码 Intent intent1=new Intent(); Intent intent1 = new Intent(); intent1 =setParam("userName","zhangsan"); presentForResult(new MainAbility2Slice(),new Intent(),2); } }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <Text ohos:id="$+id:main2" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_main2" ohos:layout_alignment="horizontal_center" ohos:text="main2" ohos:text_size="40vp" /> </DirectionalLayout>
//目标Ability2Slice 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_main2); text.setText(intent.getStringParam("userName")); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }