鸿蒙系统如何实现同步,鸿蒙系统中如何实现页面间跳转?
01认识 IntentIntent 是对象之间传递信息的载体。例如,当一个 Ability 需要启动另一个 Ability 时,或者一个 AbilitySlice 需要导航到另一个 AbilitySlice 时,可以通过 Intent 指定启动的目标同时携带相关数据。Intent 的构成元素包括 Operation 与 Parameters:02了解 AbilitySlice 路由配置虽然一个 .
01认识 Intent
Intent 是对象之间传递信息的载体。
例如,当一个 Ability 需要启动另一个 Ability 时,或者一个 AbilitySlice 需要导航到另一个 AbilitySlice 时,可以通过 Intent 指定启动的目标同时携带相关数据。Intent 的构成元素包括 Operation 与 Parameters:
02了解 AbilitySlice 路由配置
虽然一个 Page 可以包含多个 AbilitySlice,但是 Page 进入前台时界面默认只展示一个 AbilitySlice。
默认展示的 AbilitySlice 是通过 setMainRoute() 方法来指定的。
当有多个需要展示的 AbilitySlice,可以通过 addActionRoute() 方法为 MainAbilitySlice 以外的 AbilitySlice 配置路由规则。此时,当其他 Page 实例期望导航到这些 AbilitySlice 时,可以通过 AbilitySlice 之间的跳转,显示出这张页面。public class MyAbility extends Ability{ @Override public void onStart(Intent intent){ super.onStart(intent); // set the main route setMainRoute(MainSlice.class.getName()); // set the action route addActionRoute("action.pay", PaySlice.class.getName()); addActionRoute("action.scan", ScanSlice.class.getName()); }}addActionRoute() 方法中使用的动作命名,需要在应用配置文件 (config.json) 中注册:{ "module": { "abilities": [ { "skills":[ { "actions":[ "action.pay", "action.scan" ] } ] ... } ] ... } ...}
03同一个 Page 里跳转
同一个 Page 里的 AbilitySlice1 与 AbilitySlice2 间的跳转(无参,带参,回值)。
①无参数跳转@Overridepublic void onStart(Intent intent){ super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld); text.setClickedListener(component->{ Intent intent1 = new Intent(); present(new MainAbilitySlice1(),intent1); });}
②带参数跳转产生参数端的 AbilitySlice:@Overridepublic void onStart(Intent intent){ super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld); text.setClickedListener(component->{ //有参数跳转 Intent intent1 = new Intent(); intent1.setParam("user","钟发发"); present(new MainAbilitySlice1(),intent1); });}接收参数端的 AbilitySlice:public class MainAbilitySlice1 extends AbilitySlice{ Text text; String oldText; @Override public void onStart(Intent intent){ super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main1); text = (Text) findComponentById(ResourceTable.Id_text_helloworld1); if(intent != null){ String user = intent.getStringParam("user"); oldText = text.getText(); text.append("," + user); } } @Override protected void onInactive(){ super.onInactive(); }....}
③带参数跳转+返回值
参数产生端: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_text_helloworld); text.setClickedListener(component->{ //有参数跳转 Intent intent1 = new Intent(); intent1.setParam("user","钟发发"); presentForResult(new MainAbilitySlice1(),intent1,120); }); }...}参数接收端:public class MainAbilitySlice1 extends AbilitySlice{ Text text; String oldText; @Override public void onStart(Intent intent){ super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main1); text = (Text) findComponentById(ResourceTable.Id_text_helloworld1); if(intent != null){ String user = intent.getStringParam("user"); oldText = text.getText(); text.setText(oldText + "," + user); } //参数接收端在对文字点击 text.setClickedListener(component -> { //1.给跳转来的页面返回值 Intent intent1 = new Intent(); intent1.setParam("password","123456"); setResult(intent1); //2.接收本AbilityAlice,自动返回上一页 terminate(); }); } @Override protected void onInactive(){ super.onInactive(); text.setText(oldText); }...}回到参数产生端接收返回值:public class MainAbilitySlice extends AbilitySlice{ Text text; @Override public void onStart(Intent intent){ super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text = (Text)findComponentById(ResourceTable.Id_text_helloworld); text.setClickedListener(component->{ //有参数跳转 Intent intent1 = new Intent(); intent1.setParam("user","钟发发"); presentForResult(new MainAbilitySlice1(),intent1,120); }); } @Override protected void onResult(int requestCode, Intent resultIntent){ super.onResult(requestCode, resultIntent); if(requestCode==120){ String password = resultIntent.getStringParam("password"); text.setText("返回值:" + password); } }...}
04不同的 Page 直接跳转第一个鸿蒙应用例子写的就是这个,核心代码:if (button != null) { // 为按钮设置点击回调 button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component){ Intent secondIntent = new Intent(); // 指定待启动FA的bundleName和abilityName Operation operation = new Intent.OperationBuilder() .withDeviceId("") .withBundleName("com.example.myapplication") .withAbilityName("com.example.myapplication.SecondAbility") .build(); secondIntent.setOperation(operation); startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面 } });}
Page1 的 MainAbilitySlice 跳转 Page2 的 AbilitySlice1:@Overridepublic void onStart(Intent intent){ super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); text = (Text)findComponentById(ResourceTable.Id_text_helloworld); text.setClickedListener(component->{ Intent intent1 = new Intent(); intent1.setAction("abilityslice1"); //关键是配置文件里配置action和Ability里注册路由 startAbility(intent1); });}文章内容已录制成视频课程《鸿蒙手机应用开发入门》:https://edu.51cto.com/course/26133.html👇扫码关注鸿蒙技术社区👇
专注开源技术,共建鸿蒙生态点“阅读原文”了解更多
更多推荐
所有评论(0)