当前位置:   article > 正文

DevEco 页面跳转与Ability的生命周期(笔记)_deveco studio abilityslice跳转

deveco studio abilityslice跳转

一、同一页面不同切片的跳转

  • 无参跳转
  • 有参跳转
  • 有/无参有返回值跳转

 

ability_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:orientation="vertical"
  7. ohos:alignment="center"
  8. >
  9. <Button
  10. ohos:id="$+id:but1"
  11. ohos:height="match_content"
  12. ohos:width="match_content"
  13. ohos:background_element="#FF00FF"
  14. ohos:layout_alignment="horizontal_center"
  15. ohos:text="无参无返回的跳转"
  16. ohos:text_size="40vp"
  17. />
  18. <Button
  19. ohos:id="$+id:but2"
  20. ohos:height="match_content"
  21. ohos:width="match_content"
  22. ohos:background_element="#FF00FF"
  23. ohos:layout_alignment="horizontal_center"
  24. ohos:text="有参无返回的跳转"
  25. ohos:text_size="40vp"
  26. />
  27. <Button
  28. ohos:id="$+id:but3"
  29. ohos:height="match_content"
  30. ohos:width="match_content"
  31. ohos:background_element="#FF00FF"
  32. ohos:layout_alignment="horizontal_center"
  33. ohos:text="有参有返回的跳转"
  34. ohos:text_size="40vp"
  35. />
  36. </DirectionalLayout>

abilityslice1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:orientation="vertical">
  7. <Text
  8. ohos:id="$+id:text1"
  9. ohos:height="match_content"
  10. ohos:width="match_content"
  11. ohos:background_element="#FF00FF"
  12. ohos:text="无参无返回的跳转"
  13. ohos:text_size="40vp"
  14. />
  15. </DirectionalLayout>

abilityslice2.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:orientation="vertical">
  7. <Text
  8. ohos:id="$+id:text2"
  9. ohos:height="match_content"
  10. ohos:width="match_content"
  11. ohos:background_element="#FF00FF"
  12. ohos:text="有参无返回的跳转"
  13. ohos:text_size="40vp"
  14. />
  15. </DirectionalLayout>

abilityslice3.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:orientation="vertical">
  7. <Text
  8. ohos:id="$+id:text3"
  9. ohos:height="match_content"
  10. ohos:width="match_content"
  11. ohos:background_element="#FF00FF"
  12. ohos:text="有参右有返回的跳转"
  13. ohos:text_size="40vp"
  14. />
  15. </DirectionalLayout>

 新建3个页面切片

 

为config添加动作(别名) 

修改MainAbility

  1. package com.example.myapplication;
  2. import com.example.myapplication.slice.AbilitySlice1;
  3. import com.example.myapplication.slice.AbilitySlice2;
  4. import com.example.myapplication.slice.AbilitySlice3;
  5. import com.example.myapplication.slice.MainAbilitySlice;
  6. import ohos.aafwk.ability.Ability;
  7. import ohos.aafwk.content.Intent;
  8. public class MainAbility extends Ability {
  9. @Override
  10. public void onStart(Intent intent) {
  11. super.onStart(intent);
  12. //添加主要子界面
  13. super.setMainRoute(MainAbilitySlice.class.getName());
  14. //添加其他子页面
  15. super.addActionRoute("slice1",AbilitySlice1.class.getName());
  16. super.addActionRoute("slice2", AbilitySlice2.class.getName());
  17. super.addActionRoute("slice3",AbilitySlice3.class.getName());
  18. }
  19. }

MainAbilitySlice 

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Button;
  6. import ohos.agp.components.Component;
  7. import ohos.agp.window.dialog.ToastDialog;
  8. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
  9. Button but1;
  10. Button but2;
  11. Button but3;
  12. @Override
  13. public void onStart(Intent intent) {
  14. super.onStart(intent);
  15. super.setUIContent(ResourceTable.Layout_ability_main);
  16. but1 = (Button)findComponentById(ResourceTable.Id_but1);
  17. but2 = (Button)findComponentById(ResourceTable.Id_but2);
  18. but3 = (Button)findComponentById(ResourceTable.Id_but3);
  19. but1.setClickedListener(this);
  20. but2.setClickedListener(this);
  21. but3.setClickedListener(this);
  22. }
  23. @Override
  24. public void onActive() {
  25. super.onActive();
  26. }
  27. @Override
  28. public void onForeground(Intent intent) {
  29. super.onForeground(intent);
  30. }
  31. //requestCode为请求码,用来确认当前结果过是哪来的
  32. //resultIntent为结果意图,返回的意图对象
  33. @Override
  34. protected void onResult(int requestCode, Intent resultIntent) {
  35. super.onResult(requestCode, resultIntent);
  36. if(requestCode==200){
  37. //第二个参数为默认结果
  38. boolean result = resultIntent.getBooleanParam("result",false);
  39. ToastDialog td = new ToastDialog(this);
  40. if(result){
  41. td.setText("登录成功");
  42. }
  43. else{
  44. td.setText("登录失败");
  45. }
  46. td.setDuration(2000);
  47. td.show();
  48. }
  49. }
  50. @Override
  51. public void onClick(Component component) {
  52. //意图,即跳转时我们要做的事情
  53. //参数1.确定跳转时跳转到哪一个设备:operation
  54. //参数2.在跳转的时候是否需要带上参数:parameter
  55. //如果是在同一个Ability下,那么第一个参数可以不写
  56. //如果不需要参数,则也可以不写第二个参数
  57. if(component == but1){
  58. //无参无返回的跳转
  59. //创建意图对象
  60. Intent i = new Intent();
  61. //跳转
  62. present(new AbilitySlice1(),i);
  63. }
  64. else if(component == but2){
  65. //有参无返回的跳转
  66. Intent i = new Intent();
  67. i.setParam("username","zhangsan");
  68. i.setParam("password","123456");
  69. present(new AbilitySlice2(),i);
  70. }
  71. else if(component == but3){
  72. //有参有返回的跳转
  73. Intent i = new Intent();
  74. i.setParam("username","zhangsan");
  75. i.setParam("password","123456");
  76. //请求码自己设置
  77. presentForResult(new AbilitySlice3(),i,200);
  78. }
  79. }
  80. }

 AbilitySlice1

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. public class AbilitySlice1 extends AbilitySlice {
  6. @Override
  7. public void onStart(Intent intent) {
  8. super.onStart(intent);
  9. super.setUIContent(ResourceTable.Layout_abilityslice1);
  10. }
  11. @Override
  12. public void onActive() {
  13. super.onActive();
  14. }
  15. @Override
  16. public void onForeground(Intent intent) {
  17. super.onForeground(intent);
  18. }
  19. }

 AbilitySlice2

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Text;
  6. public class AbilitySlice2 extends AbilitySlice {
  7. @Override
  8. public void onStart(Intent intent) {
  9. super.onStart(intent);
  10. super.setUIContent(ResourceTable.Layout_abilityslice2);
  11. String unm = intent.getStringParam("username");
  12. String pwd = intent.getStringParam("password");
  13. Text text = (Text)findComponentById(ResourceTable.Id_text2);
  14. text.setText("你好,"+unm+"!密码是:"+pwd);
  15. }
  16. @Override
  17. public void onActive() {
  18. super.onActive();
  19. }
  20. @Override
  21. public void onForeground(Intent intent) {
  22. super.onForeground(intent);
  23. }
  24. }

 AbilitySlice3

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Component;
  6. import ohos.agp.components.Text;
  7. public class AbilitySlice3 extends AbilitySlice{
  8. @Override
  9. public void onStart(Intent intent) {
  10. super.onStart(intent);
  11. super.setUIContent(ResourceTable.Layout_abilityslice3);
  12. Text text = (Text)findComponentById(ResourceTable.Id_text3);
  13. String unm = intent.getStringParam("username");
  14. String pwd = intent.getStringParam("password");
  15. Intent i = new Intent();
  16. if(pwd=="123456") {
  17. text.setText("你好,"+unm+"即将跳转");
  18. i.setParam("result",true);
  19. //返回意图
  20. setResult(i);
  21. //关闭当前页面
  22. terminate();
  23. }
  24. else{
  25. i.setParam("result",false);
  26. //返回意图
  27. setResult(i);
  28. //关闭当前页面
  29. terminate();
  30. }
  31. }
  32. @Override
  33. public void onActive() {
  34. super.onActive();
  35. }
  36. @Override
  37. public void onForeground(Intent intent) {
  38. super.onForeground(intent);
  39. }
  40. }

二、不同页面的跳转

  • A页面跳转到B页面的主页面
  • A页面跳转到B页面的其他页面

搭配一部分页面切片间的跳转方式即:

  • 无参跳转
  • 有参跳转
  • 有/无参有返回值跳转

创建PageAbility(不要勾选LaunchAbility:指的是APP主页面)

 准备两个Ability,三个切片(两个主切片+一个子切片)

 别忘了将子切片绑定在第二个主页面上,同时为其注册一个行动

 

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.aafwk.content.Operation;
  6. import ohos.agp.components.Button;
  7. import ohos.agp.components.Component;
  8. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
  9. Button but1;
  10. Button but2;
  11. @Override
  12. public void onStart(Intent intent) {
  13. super.onStart(intent);
  14. super.setUIContent(ResourceTable.Layout_ability_main);
  15. but1 = (Button)findComponentById(ResourceTable.Id_but1);
  16. but2 = (Button)findComponentById(ResourceTable.Id_but2);
  17. but1.setClickedListener(this);
  18. but2.setClickedListener(this);
  19. }
  20. @Override
  21. public void onActive() {
  22. super.onActive();
  23. }
  24. @Override
  25. public void onForeground(Intent intent) {
  26. super.onForeground(intent);
  27. }
  28. @Override
  29. public void onClick(Component component) {
  30. if(component == but1){
  31. Intent i = new Intent();
  32. //OperationBuilder拼接参数
  33. //withDeviceId跳转的机器ID,“”表示本机
  34. //withBundleName哪一个应用上(config.json中的BundleName)
  35. //withAbilityName哪一个页面上(config.json中的name)
  36. //此时默认跳转到页面的主切面
  37. Operation oper = new Intent.OperationBuilder()
  38. .withDeviceId("")
  39. .withBundleName("com.example.myapplication.hmservice")
  40. .withAbilityName("com.example.myapplication.MainAbility2")
  41. .build();
  42. i.setOperation(oper);
  43. //i.setParam("test","test");
  44. startAbility(i);
  45. //startAbilityForResult(i,200);
  46. }
  47. else if(component == but2){
  48. //由于我们这里在config.json中注册了的,且在同一个APP中
  49. //因此可以用省略写法,只写有用参数,即withAction
  50. Intent i = new Intent();
  51. Operation oper = new Intent.OperationBuilder()
  52. .withAction("slice2")
  53. .build();
  54. i.setOperation(oper);
  55. startAbility(i);
  56. }
  57. }
  58. }

三、Ability生命周期

  • 初始状态(Initial)
  • 闲置状态(Inactive)
  • 活跃状态(Active)
  • 隐藏状态(Background)

过程: 

  1. 初始状态:未加载组件
  2. OnStart():加载XML,仅会执行一次
  3. 闲置状态:在内存中,但未被展示
  4. onActice():展示页面
  5. 活跃状态:与用户交互
  6. onInactive():失去交点
  7. 闲置状态:在内存中,但未被展示,等待再次使用(onActice())或者等待被放入回收区(onBackground())
  8. onBackground():当不再使用时,放入可被回收区域
  9. 隐藏状态:等待回收(kill)、重新激活 (onForeground())、结束进程(stop())
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号