美文网首页
Page Ability生命周期

Page Ability生命周期

作者: 裴云飞 | 来源:发表于2020-10-13 21:07 被阅读0次

    Page 生命周期回调

    Page生命周期,原谅我无耻的从官网盗了一张图
    • onStart()
      当系统首次创建Page实例时,触发该回调。对于一个Page实例,该回调在其生命周期过程中仅触发一次,Page在该逻辑后将进入非活跃状态。
    • onActive()
      Page 会在进入非活跃状态后来到前台,然后系统调用此回调。Page 在此之后进入 活跃状态,该状态是应用与用户交互的状态。当另一个页面来到前台,会触发 Page 回到非活跃状态,系统将调用 onInactive() 回调。此后,Page 可能重新回到活跃状态,系统将再次调用 onActive() 回调。
    • onInactive()
      当Page失去焦点时,系统将调用此回调,此后Page进入非活跃状态。
    • onBackground()
      如果Page不再对用户可见,系统将调用此回调通知开发者用户进行相应的资源释放,此后Page进入后台状态。
    • onForeground()
      处于后台状态的Page仍然驻留在内存中,当重新回到前台时(比如用户重新导航到此Page),系统将先调用onForeground()回调通知开发者,而后Page的生命周期状态回到非活跃状态,最后Page的生命周期状态进一步回到活跃状态,系统将通过onActive()回调通知开发者用户。
    • onStop()
      系统将要销毁 Page 时,将会触发此回调函数,通知用户进行系统资源的释放。销毁 Page 的可能原因包括以下几个方面:
      • 用户通过系统管理能力关闭指定 Page,例如使用任务管理器关闭 Page。
      • 用户行为触发 Page 的 terminateAbility() 方法调用,例如使用应用的退出功能。
      • 配置变更导致系统暂时销毁 Page 并重建。
      • 系统出于资源管理目的,自动触发对处于后台状态 Page 的销毁。

    AbilitySlice生命周期

    AbilitySlice作为Page的组成单元,其生命周期是依托于其所属Page生命周期的。AbilitySlice和Page具有相同的生命周期状态和同名的回调,当Page生命周期发生变化时,它的AbilitySlice也会发生相同的生命周期变化。此外,AbilitySlice还具有独立于Page的生命周期变化,这发生在同一Page中的AbilitySlice之间导航时,此时Page的生命周期状态不会改变。

    页面跳转的生命周期回调

    为了便于理解,我们写个案例,MainAbility有两个slice,分别是MainAbilitySlice和MainOtherAbilitySlice。另外还有个SecondAbility,SecondAbility的slice叫做SecondAbilitySlice。

    public class MainAbility extends Ability {
    
        private HiLogLabel mHiLogLabel = new HiLogLabel(HiLog.DEBUG, 0x00101, "MainAbility");
    
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            super.setMainRoute(MainAbilitySlice.class.getName());
            HiLog.info(mHiLogLabel, "onStart");
        }
    
        @Override
        protected void onActive() {
            super.onActive();
            HiLog.info(mHiLogLabel, "onActive");
        }
    
        @Override
        protected void onInactive() {
            super.onInactive();
            HiLog.info(mHiLogLabel, "onStart");
        }
    
        @Override
        protected void onBackground() {
            super.onBackground();
            HiLog.info(mHiLogLabel, "onBackground");
        }
    
        @Override
        protected void onForeground(Intent intent) {
            super.onForeground(intent);
            HiLog.info(mHiLogLabel, "onForeground");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            HiLog.info(mHiLogLabel, "onStop");
        }
    }
    

    MainAbility里面的MainAbilitySlice

    public class MainAbilitySlice extends AbilitySlice {
    
        private HiLogLabel mHiLogLabel = new HiLogLabel(HiLog.DEBUG, 0x00101, "MainAbilitySlice");
        public static final int PRESENT_REQUEST_CODE = 0x01;
        public static final int INTENT_REQUEST_CODE = 0x02;
        private Text mText;
        private Text same;
        private Text next;
    
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            super.setUIContent(ResourceTable.Layout_main_ability);
            HiLog.info(mHiLogLabel, "onStart");
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://api.github.com/repos/JetBrains/Kotlin")
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    System.out.println("MainAbilitySlice" + e.getMessage());
                }
    
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    System.out.println("MainAbilitySlice" + response.body().string());
                }
            });
        }
    
        @Override
        public void onActive() {
            super.onActive();
            HiLog.info(mHiLogLabel, "onActive");
            same = (Text) findComponentById(ResourceTable.Id_same);
            next = (Text) findComponentById(ResourceTable.Id_next);
            mText = (Text) findComponentById(ResourceTable.Id_text);
            same.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Intent intent = new Intent();
                    intent.setParam("user", "您好");
                    presentForResult(new MainOtherAbilitySlice(), intent, PRESENT_REQUEST_CODE);
                }
            });
            next.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Intent intent = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName("com.example.abilityslicenavigation")
                            .withAbilityName("com.example.abilityslicenavigation.SecondAbility")
                            .build();
                    intent.setParam("user", "您好");
                    intent.setOperation(operation);
                    startAbilityForResult(intent, INTENT_REQUEST_CODE);
                }
            });
        }
    
        @Override
        public void onForeground(Intent intent) {
            super.onForeground(intent);
            HiLog.info(mHiLogLabel, "onForeground");
        }
    
        @Override
        protected void onBackground() {
            super.onBackground();
            HiLog.info(mHiLogLabel, "onBackground");
        }
    
        @Override
        protected void onInactive() {
            super.onInactive();
            HiLog.info(mHiLogLabel, "onInactive");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            HiLog.info(mHiLogLabel, "onStop");
        }
    }
    

    MainAbility里面的MainOtherAbilitySlice

    public class MainOtherAbilitySlice extends AbilitySlice {
    
        private HiLogLabel mHiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainOtherAbilitySlice");
        private DirectionalLayout myLayout = new DirectionalLayout(this);
    
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            HiLog.info(mHiLogLabel, "onStart");
            LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
            myLayout.setLayoutConfig(config);
            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(255, 255, 255));
            myLayout.setBackground(element);
            Text text = new Text(this);
            text.setLayoutConfig(config);
            text.setText("同一个页面的另一个AbilitySlice");
            text.setTextColor(new Color(0xFF000000));
            text.setTextSize(50);
            text.setTextAlignment(TextAlignment.CENTER);
            myLayout.addComponent(text);
            super.setUIContent(myLayout);
        }
    
        @Override
        public void onActive() {
            super.onActive();
            HiLog.info(mHiLogLabel, "onActive");
        }
    
        @Override
        public void onForeground(Intent intent) {
            super.onForeground(intent);
            HiLog.info(mHiLogLabel, "onForeground");
        }
    
        @Override
        protected void onInactive() {
            super.onInactive();
            HiLog.info(mHiLogLabel, "onInactive");
        }
    
        @Override
        protected void onBackground() {
            super.onBackground();
            HiLog.info(mHiLogLabel, "onBackground");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            HiLog.info(mHiLogLabel, "onStop");
        }
    }
    
    public class SecondAbility extends Ability {
    
        private HiLogLabel mHiLogLabel = new HiLogLabel(HiLog.DEBUG, 0x00101, "SecondAbility");
    
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            super.setMainRoute(SecondAbilitySlice.class.getName());
            HiLog.info(mHiLogLabel, "onStart");
        }
    
        @Override
        protected void onActive() {
            super.onActive();
            HiLog.info(mHiLogLabel, "onActive");
        }
    
        @Override
        protected void onInactive() {
            super.onInactive();
            HiLog.info(mHiLogLabel, "onStart");
        }
    
        @Override
        protected void onBackground() {
            super.onBackground();
            HiLog.info(mHiLogLabel, "onBackground");
        }
    
        @Override
        protected void onForeground(Intent intent) {
            super.onForeground(intent);
            HiLog.info(mHiLogLabel, "onForeground");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            HiLog.info(mHiLogLabel, "onStop");
        }
    }
    

    SecondAbility里面的SecondAbilitySlice

    public class SecondAbilitySlice extends AbilitySlice {
    
        private HiLogLabel mHiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00101, "SecondAbilitySlice");
    
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            HiLog.info(mHiLogLabel, "onStart");
            super.setUIContent(ResourceTable.Layout_second_ability);
        }
    
        @Override
        public void onActive() {
            super.onActive();
            HiLog.info(mHiLogLabel, "onActive");
        }
    
        @Override
        public void onForeground(Intent intent) {
            super.onForeground(intent);
            HiLog.info(mHiLogLabel, "onForeground");
        }
    
        @Override
        protected void onBackground() {
            super.onBackground();
            HiLog.info(mHiLogLabel, "onBackground");
        }
    
        @Override
        protected void onInactive() {
            super.onInactive();
            HiLog.info(mHiLogLabel, "onInactive");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            HiLog.info(mHiLogLabel, "onStop");
        }
    }
    

    1、运行项目,首先启动MainAbility的MainAbilitySlice,生命周期的调用顺序为:
    MainAbility.onStart() --> MainAbilitySlice.onStart() --> MainAbility.onActive() --> MainAbilitySlice.onActive()

    2、跳转到MainAbility的MainOtherAbilitySlice,生命周期的调用顺序为:
    MainAbilitySlice.onInactive() --> MainOtherAbilitySlice.onStart() --> MainOtherAbilitySlice.onActive() --> MainAbilitySlice.onBackground()

    3、关闭MainAbility的MainOtherAbilitySlice,生命周期的调用顺序为:
    MainOtherAbilitySlice.onInactive() --> MainAbilitySlice.onForeground() --> MainAbilitySlice.onActive() --> MainOtherAbilitySlice.onBackground() --> MainOtherAbilitySlice.onStop()

    同一Page中的AbilitySlice之间导航,Page的生命周期状态不会改变。在这个流程中,MainAbility始终处于活跃状态。

    4、在MainAbility中启动SecondAbility的SecondAbilitySlice,生命周期的调用顺序为:
    MainAbility.onStart() --> MainAbilitySlice.onInactive() --> SecondAbility.onStart() --> SecondAbilitySlice.onStart() --> SecondAbility.onActive() --> SecondAbilitySlice.onActive() --> MainAbility.onBackground() --> MainAbilitySlice.onBackground()

    5、关闭SecondAbility的SecondAbilitySlice,生命周期的调用顺序为:
    SecondAbility.onStart() --> SecondAbilitySlice.onInactive() --> MainAbility.onForeground() --> MainAbilitySlice.onForeground() --> MainAbility.onActive() --> MainAbilitySlice.onActive() --> SecondAbility.onBackground() --> SecondAbilitySlice.onBackground() --> SecondAbility.onStop() --> SecondAbilitySlice.onStop()

    6、关闭MainAbility的MainAbilitySlice,生命周期的调用顺序为:
    MainAbility.onStart() --> MainAbilitySlice.onInactive()

    相关文章

      网友评论

          本文标题:Page Ability生命周期

          本文链接:https://www.haomeiwen.com/subject/myuvpktx.html