Page Ability生命周期

2020-10-13  本文已影响0人  裴云飞

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()

上一篇下一篇

猜你喜欢

热点阅读