当前位置:   article > 正文

5、PageAbility的生命周期_下面哪些是ability的生命周期

下面哪些是ability的生命周期

作者:韩茹

公司:程序咖(北京)科技有限公司

鸿蒙巴士专栏作家

系统管理或用户操作等行为均会引起Page实例在其生命周期的不同状态之间进行转换。Ability类提供的回调机制能够让Page及时感知外界变化,从而正确地应对状态变化(比如释放资源),这有助于提升应用的性能和稳健性。

一、Page生命周期回调

Page Ability是主要负责页面交互的,所以Page有几个状态:可见,可交互,不可见,销毁等等。每一个状态,都有一个生命周期函数和它对应。Page生命周期的不同状态转换及其对应的回调,如下图所示所示。(图片来自官网)

0000000000011111111.20210330203818.05372477209167764226774078468340

  • onStart()

    当系统首次创建Page实例时,触发该回调。对于一个Page实例,该回调在其生命周期过程中仅触发一次,Page在该逻辑后将进入INACTIVE状态。开发者必须重写该方法,并在此配置默认展示的AbilitySlice。

        @Override    
        public void onStart(Intent intent) {        
        	super.onStart(intent);        
        	super.setMainRoute(FooSlice.class.getName());    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • onActive()

    Page会在进入INACTIVE状态后来到前台,然后系统调用此回调。Page在此之后进入ACTIVE状态,该状态是应用与用户交互的状态。Page将保持在此状态,除非某类事件发生导致Page失去焦点,比如用户点击返回键或导航到其他Page。当此类事件发生时,会触发Page回到INACTIVE状态,系统将调用onInactive()回调。此后,Page可能重新回到ACTIVE状态,系统将再次调用onActive()回调。因此,开发者通常需要成对实现onActive()和onInactive(),并在onActive()中获取在onInactive()中被释放的资源。

  • onInactive()

    当Page失去焦点时,系统将调用此回调,此后Page进入INACTIVE状态。开发者可以在此回调中实现Page失去焦点时应表现的恰当行为。

  • onBackground()

    如果Page不再对用户可见,系统将调用此回调通知开发者用户进行相应的资源释放,此后Page进入BACKGROUND状态。开发者应该在此回调中释放Page不可见时无用的资源,或在此回调中执行较为耗时的状态保存操作。

  • onForeground()

    处于BACKGROUND状态的Page仍然驻留在内存中,当重新回到前台时(比如用户重新导航到此Page),系统将先调用onForeground()回调通知开发者,而后Page的生命周期状态回到INACTIVE状态。开发者应当在此回调中重新申请在onBackground()中释放的资源,最后Page的生命周期状态进一步回到ACTIVE状态,系统将通过onActive()回调通知开发者用户。

  • onStop()

    系统将要销毁Page时,将会触发此回调函数,通知用户进行系统资源的释放。销毁Page的可能原因包括以下几个方面:

    • 用户通过系统管理能力关闭指定Page,例如使用任务管理器关闭Page。
    • 用户行为触发Page的terminateAbility()方法调用,例如使用应用的退出功能。
    • 配置变更导致系统暂时销毁Page并重建。
    • 系统出于资源管理目的,自动触发对处于BACKGROUND状态Page的销毁。

示例代码:

我们在MainAbility.java中重写以上生命周期方法,并打印HiLog日志:

package com.example.hanrupageabilitylifecycle;

import com.example.hanrupageabilitylifecycle.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.PacMap;

public class MainAbility extends Ability {

    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG_ABILITY");

    // 1.当系统首次创建 Page Ability实例时,触发该回调。将进入 INACTIVE 状态
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        HiLog.info(LABEL, "==MainAbility--->onStart()");
    }

    // 2.Page Ability 在此之后进入ACTIVE 状态,该状态是应用与用户交互的状态。
    @Override
    protected void onActive() {
        super.onActive();
        HiLog.info(LABEL, "==MainAbility--->onActive()");
    }

    // 3.当 Page Ability失去焦点时,系统将调用此回调,此后 Page 进入 INACTIVE 状态。
    @Override
    protected void onInactive() {
        super.onInactive();
        HiLog.info(LABEL, "==MainAbility--->onInactive()");
    }

    // 4.如果 Page Ability不再对用户可见,系统将调用此回调通知开发者用户进行相应的资源释放,此后Page Ability进入 BACKGROUND 状态。
    @Override
    protected void onBackground() {
        super.onBackground();
        HiLog.info(LABEL, "==MainAbility--->onBackground()");
    }

    // 5.处于 BACKGROUND 状态的 Page Ability仍然驻留在内存中,当重新回到前台时(比如用户重新导航到此 Page Ability),系统将先调用 onForeground()回调通知开发者,而后 Page 的生命周期状态回到 INACTIVE 状态。
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        HiLog.info(LABEL, "==MainAbility--->onForeground()");
    }

    // 6.系统将要销毁 Page Ability时,将会触发此回调函数,通知用户进行系统资源的释放。
    @Override
    protected void onStop() {
        super.onStop();
        HiLog.info(LABEL, "==MainAbility--->onStop()");
    }


}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

当我们启动项目,项目默认打印HelloWorld:

WX20210625-164821@2x

终端打印:

WX20210625-164620@2x

06-25 16:46:06.165 18347-18347/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 16:46:06.221 18347-18347/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()

  • 1
  • 2
  • 3

到这里,我们可以看到,当一个项目启动后,根据config.json配置文件,首先加载MainAbility,会先执行里面的onStart()和onActive()两个方法。

此时,然后我们按返回键:

然后先后执行了:onInactive()–>onBackground()–>onStop()

WX20210625-165020@2x

06-25 16:50:03.231 18347-18347/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onInactive()
06-25 16:50:05.153 18347-18347/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onBackground()
06-25 16:50:05.166 18347-18347/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStop()
  • 1
  • 2
  • 3

二、AbilitySlice生命周期

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

AbilitySlice生命周期回调与Page的相应回调类似,因此不再赘述。由于AbilitySlice承载具体的页面,开发者必须重写AbilitySlice的onStart()回调,并在此方法中通过setUIContent()方法设置页面,如下所示:

    @Override    
    protected void onStart(Intent intent) {        
    	super.onStart(intent);
      setUIContent(ResourceTable.Layout_main_layout);    
    }
  • 1
  • 2
  • 3
  • 4
  • 5

AbilitySlice实例创建和管理通常由应用负责,系统仅在特定情况下会创建AbilitySlice实例。例如,通过导航启动某个AbilitySlice时,是由系统负责实例化;但是在同一个Page中不同的AbilitySlice间导航时则由应用负责实例化。

现在我们在AbilitySlice中也重写对应的生命周期方法:

package com.example.hanrupageabilitylifecycle.slice;

import com.example.hanrupageabilitylifecycle.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG_ABILITY_SLICE");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        HiLog.info(LABEL, "==MainAbilitySlice--->onStart()");
    }

    @Override
    public void onActive() {
        super.onActive();
        HiLog.info(LABEL, "==MainAbilitySlice--->onActive()");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        HiLog.info(LABEL, "==MainAbilitySlice--->onForeground()");

    }

    @Override
    protected void onInactive() {
        super.onInactive();
        HiLog.info(LABEL, "==MainAbilitySlice--->onInactive()");
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        HiLog.info(LABEL, "==MainAbilitySlice--->onBackground()");
    }



    @Override
    protected void onStop() {
        super.onStop();
        HiLog.info(LABEL, "==MainAbilitySlice--->onStop()");
    }


}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

启动项目后:

WX20210625-170555@2x

06-25 17:05:02.011 3685-3685/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 17:05:02.031 3685-3685/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onStart()
06-25 17:05:02.041 3685-3685/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
06-25 17:05:02.041 3685-3685/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onActive()
  • 1
  • 2
  • 3
  • 4

按下返回键:

WX20210625-170910@2x

06-25 17:08:35.311 2509-2509/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onInactive()
06-25 17:08:35.311 2509-2509/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onInactive()
06-25 17:08:36.695 2509-2509/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onBackground()
06-25 17:08:36.696 2509-2509/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onBackground()
06-25 17:08:36.701 2509-2509/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStop()
06-25 17:08:36.702 2509-2509/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onStop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

三、Page与AbilitySlice生命周期关联

当AbilitySlice处于前台且具有焦点时,其生命周期状态随着所属Page的生命周期状态的变化而变化。当一个Page拥有多个AbilitySlice时,例如:MyAbility下有MainAbilitySlice和SecondAbilitySlice,当前MainAbilitySlice处于前台并获得焦点,并即将导航到SecondAbilitySlice,在此期间的生命周期状态变化顺序为:

  1. MainAbilitySlice从ACTIVE状态变为INACTIVE状态。
  2. SecondAbilitySlice则从INITIAL状态首先变为INACTIVE状态,然后变为ACTIVE状态(假定此前SecondAbilitySlice未曾启动)。
  3. MainAbilitySlice从INACTIVE状态变为BACKGROUND状态。

对应两个slice的生命周期方法回调顺序为:

MainAbilitySlice.onInactive() --> SecondAbilitySlice.onStart() --> SecondAbilitySlice.onActive() --> MainAbilitySlicee.onBackground()

在整个流程中,MyAbility始终处于ACTIVE状态。但是,当Page被系统销毁时,其所有已实例化的AbilitySlice将联动销毁,而不仅是处于前台的AbilitySlice。

我们通过代码来实现一下,打印观察日志。

我们在MainAbilitySlice中添加一个按钮,点击后跳转到同一个Ability中的第二个AbilitySlice:

首先修改一下ability_main.xml:

<?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:padding="10vp"
    ohos:background_element="#22aa0000"
    ohos:orientation="vertical">

    <Text
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="MainAbility"
        ohos:text_size="40vp"
        ohos:layout_alignment="horizontal_center"
        ohos:bottom_margin="20vp"
        />


    <Button
        ohos:id="$+id:btn1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="跳转到第二个AbilitySlice"
        ohos:text_size="25fp"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        />

</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

改变一下页面颜色,以及添加一个按钮。

然后我们再新建一个xml布局文件:ability_second.xml

<?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:background_element="#2200aa00"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="horizontal_center"
        ohos:text="SecondAbilitySlice"
        ohos:text_size="40vp"
        />

</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

然后在slice目录下新建一个AbilitySlice:SecondAbilitySlice.java,并重写各个生命周期方法:

package com.example.hanrupageabilitylifecycle.slice;

import com.example.hanrupageabilitylifecycle.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class SecondAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG_ABILITY_SLICE");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);
        HiLog.info(LABEL, "==SecondAbilitySlice--->onStart()");
    }


    @Override
    public void onActive() {
        super.onActive();
        HiLog.info(LABEL, "==SecondAbilitySlice--->onActive()");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        HiLog.info(LABEL, "==SecondAbilitySlice--->onForeground()");

    }

    @Override
    protected void onInactive() {
        super.onInactive();
        HiLog.info(LABEL, "==SecondAbilitySlice--->onInactive()");
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        HiLog.info(LABEL, "==SecondAbilitySlice--->onBackground()");
    }



    @Override
    protected void onStop() {
        super.onStop();
        HiLog.info(LABEL, "==SecondAbilitySlice--->onStop()");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

然后在MainAbilitySlice中处理按钮的点击事件:

 				// 点击按钮,跳转到SecondAbility,观察生命周期变化
        Button btn1 = (Button) findComponentById(ResourceTable.Id_btn1);
        btn1.setClickedListener(component -> present(new SecondAbilitySlice(),new Intent()));

  • 1
  • 2
  • 3
  • 4

然后我么启动程序,

WX20210625-172127@2x

观察日志:

WX20210625-172338@2x

06-25 17:23:07.026 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 17:23:07.046 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onStart()
06-25 17:23:07.061 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
06-25 17:23:07.061 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onActive()
  • 1
  • 2
  • 3
  • 4

然后我们点击按钮,跳转到第二个AbilitySlice,观察日志:

WX20210625-172409@2x

06-25 17:23:42.768 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onInactive()
06-25 17:23:42.782 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==SecondAbilitySlice--->onStart()
06-25 17:23:42.783 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==SecondAbilitySlice--->onActive()
06-25 17:23:42.783 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onBackground()
  • 1
  • 2
  • 3
  • 4

然后我们再按手机模拟器的返回键,这个时候,会退回到第一个页面,观察日志:

WX20210625-172442@2x

06-25 17:24:17.626 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==SecondAbilitySlice--->onInactive()
06-25 17:24:17.627 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onForeground()
06-25 17:24:17.629 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onActive()
06-25 17:24:17.629 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==SecondAbilitySlice--->onBackground()
06-25 17:24:17.629 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==SecondAbilitySlice--->onStop()
  • 1
  • 2
  • 3
  • 4
  • 5

然后我们继续按返回键,会返回到手机桌面,观察日志:

WX20210625-172515@2x

06-25 17:24:59.862 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onInactive()
06-25 17:24:59.862 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onInactive()
06-25 17:25:01.214 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onBackground()
06-25 17:25:01.214 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onBackground()
06-25 17:25:01.220 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStop()
06-25 17:25:01.221 28071-28071/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onStop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

四、Page之间切换

下面我们来观察一下不同的Page Ability之间切换的时候,生命周期,首先新建一个Ability:OtherAbility.java:

WX20210625-180639@2x

并在里面重写生命周期的方法:

package com.example.hanrupageabilitylifecycle;

import com.example.hanrupageabilitylifecycle.slice.OtherAbilitySlice;
import com.example.hanrupageabilitylifecycle.slice.OtherAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class OtherAbility extends Ability {
  

    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG_ABILITY");

    // 1.当系统首次创建 Page Ability实例时,触发该回调。将进入 INACTIVE 状态
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(OtherAbilitySlice.class.getName());
        HiLog.info(LABEL, "==OtherAbility--->onStart()");
    }

    // 2.Page Ability 在此之后进入ACTIVE 状态,该状态是应用与用户交互的状态。
    @Override
    protected void onActive() {
        super.onActive();
        HiLog.info(LABEL, "==OtherAbility--->onActive()");
    }

    // 3.当 Page Ability失去焦点时,系统将调用此回调,此后 Page 进入 INACTIVE 状态。
    @Override
    protected void onInactive() {
        super.onInactive();
        HiLog.info(LABEL, "==OtherAbility--->onInactive()");
    }

    // 4.如果 Page Ability不再对用户可见,系统将调用此回调通知开发者用户进行相应的资源释放,此后Page Ability进入 BACKGROUND 状态。
    @Override
    protected void onBackground() {
        super.onBackground();
        HiLog.info(LABEL, "==OtherAbility--->onBackground()");
    }

    // 5.处于 BACKGROUND 状态的 Page Ability仍然驻留在内存中,当重新回到前台时(比如用户重新导航到此 Page Ability),系统将先调用 onForeground()回调通知开发者,而后 Page 的生命周期状态回到 INACTIVE 状态。
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        HiLog.info(LABEL, "==OtherAbility--->onForeground()");
    }

    // 6.系统将要销毁 Page Ability时,将会触发此回调函数,通知用户进行系统资源的释放。
    @Override
    protected void onStop() {
        super.onStop();
        HiLog.info(LABEL, "==OtherAbility--->onStop()");
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

然后在生成的OtherAbilitySlice中重写生命周期方法:

package com.example.hanrupageabilitylifecycle.slice;

import com.example.hanrupageabilitylifecycle.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class OtherAbilitySlice extends AbilitySlice {
    
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG_ABILITY_SLICE");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_other);
        HiLog.info(LABEL, "==OtherAbilitySlice--->onStart()");
    }


    @Override
    public void onActive() {
        super.onActive();
        HiLog.info(LABEL, "==OtherAbilitySlice--->onActive()");
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        HiLog.info(LABEL, "==OtherAbilitySlice--->onForeground()");

    }

    @Override
    protected void onInactive() {
        super.onInactive();
        HiLog.info(LABEL, "==OtherAbilitySlice--->onInactive()");
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        HiLog.info(LABEL, "==OtherAbilitySlice--->onBackground()");
    }



    @Override
    protected void onStop() {
        super.onStop();
        HiLog.info(LABEL, "==OtherAbilitySlice--->onStop()");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

修改layout下生成的ability_other.xml文件:

<?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:background_element="#220000ff"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="horizontal_center"
        ohos:text="OtherAbility"
        ohos:text_size="40vp"
        />

</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

在ability_main.xml中添加一个按钮:

<?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:padding="10vp"
    ohos:background_element="#22aa0000"
    ohos:orientation="vertical">

		...
    <Button
        ohos:id="$+id:btn2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="跳转到另一个Ability"
        ohos:top_margin="20vp"
        ohos:text_size="25fp"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        />

</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

然后在MainAbilitySlice中添加按钮的点击事件:

// 点击按钮,跳转到OtherAbility,观察生命周期
        Button btn2 = (Button) findComponentById(ResourceTable.Id_btn2);
        btn2.setClickedListener(component -> {
            //不同Page之间的导航,不能使用present()或者presentForResult()
            Intent intent1 = new Intent();
            // 通过withAbilityName()指定要跳转到Ability,但是需要同时使用withBundleName()。
            Operation operation = new Intent.OperationBuilder()
                    .withAbilityName(OtherAbility.class)
                    .withBundleName("com.example.hanrupageabilitylifecycle")
                    .build();
            intent1.setOperation(operation);
            startAbility(intent1);

        });

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

然后启动程序:

WX20210625-175357@2x

观察HiLog:

WX20210625-175347@2x

06-25 17:53:27.650 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 17:53:27.675 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onStart()
06-25 17:53:27.700 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
06-25 17:53:27.701 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onActive()
  • 1
  • 2
  • 3
  • 4

然后点击按钮,跳转到OtherAbility:

WX20210625-175437@2x

观察HiLog:

WX20210625-175425@2x

06-25 17:54:02.833 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onInactive()
06-25 17:54:02.833 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onInactive()
06-25 17:54:02.928 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==OtherAbility--->onStart()
06-25 17:54:02.936 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==OtherAbilitySlice--->onStart()
06-25 17:54:02.971 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==OtherAbility--->onActive()
06-25 17:54:02.971 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==OtherAbilitySlice--->onActive()
06-25 17:54:03.805 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onBackground()
06-25 17:54:03.806 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onBackground()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

然后按手机模拟器的返回键,返回到第一个界面,观察HiLog:

WX20210625-175531@2x

06-25 17:54:52.373 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==OtherAbility--->onInactive()
06-25 17:54:52.373 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==OtherAbilitySlice--->onInactive()
06-25 17:54:52.391 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onForeground()
06-25 17:54:52.391 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onForeground()
06-25 17:54:52.394 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
06-25 17:54:52.394 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onActive()
06-25 17:54:53.166 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==OtherAbility--->onBackground()
06-25 17:54:53.166 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==OtherAbilitySlice--->onBackground()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

继续按返回键,返回到桌面:

WX20210625-175610@2x


06-25 17:54:53.168 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==OtherAbility--->onStop()
06-25 17:54:53.168 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==OtherAbilitySlice--->onStop()
06-25 17:55:45.057 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onInactive()
06-25 17:55:45.057 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onInactive()
06-25 17:55:46.363 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onBackground()
06-25 17:55:46.363 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onBackground()
06-25 17:55:46.365 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStop()
06-25 17:55:46.365 18293-18293/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY_SLICE: ==MainAbilitySlice--->onStop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

五、横竖屏切换

当手机横竖屏切换到时候,我们看一下如何执行生命周期函数。

我们打算在MainAbility设置一个成员变量num,然后设计一个按钮,每当点击按钮,num数值累加,并将数值显示到页面上。然后我们横竖屏切换时,观察一下num数值到变化。

首先我们修改一下MainAbility中的代码,我们让MainAbility直接加载xml布局文件,并添加一个成员变量num:

public class MainAbility extends Ability {

    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG_ABILITY");

    private int num = 100;//成员变量

    // 1.当系统首次创建 Page Ability实例时,触发该回调。将进入 INACTIVE 状态
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(MainAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_main);


    }
  ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

然后我么修改一下ability_main:

<?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:padding="10vp"
    ohos:background_element="#22aa0000"
    ohos:orientation="vertical">

    <Text
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="MainAbility"
        ohos:text_size="40vp"
        ohos:layout_alignment="horizontal_center"
        ohos:bottom_margin="20vp"
        />
    <Button
        ohos:id="$+id:btn3"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="加血加血"
        ohos:top_margin="20vp"
        ohos:text_size="25fp"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        />

    <Text
        ohos:id="$+id:text_num"
        ohos:height="100vp"
        ohos:width="300vp"
        ohos:text_size="25fp"
        ohos:text="血量num:100"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="#2200ff00"
        ohos:top_margin="20vp"
        ohos:text_alignment="center"
        />



    <Button
        ohos:id="$+id:btn1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="跳转到第二个AbilitySlice"
        ohos:text_size="25fp"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        />
    <Button
        ohos:id="$+id:btn2"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="跳转到另一个Ability"
        ohos:top_margin="20vp"
        ohos:text_size="25fp"
        ohos:background_element="#EEEEEE"
        ohos:padding="10vp"
        />



</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

我们新增一个按钮3,以及一个text。

然后我们在MainAbility中的onStart()中处理一下按钮的点击事件:

 	public void onStart(Intent intent) {
        super.onStart(intent);
        //super.setMainRoute(MainAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_main);

        initComponent();

        btn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                num++;
                textNum.setText("数值num:"+num);
            }
        });

        HiLog.info(LABEL, "==MainAbility--->onStart()");
    }
		private void initComponent(){
        btn = (Button) findComponentById(ResourceTable.Id_btn3);
        textNum = (Text) findComponentById(ResourceTable.Id_text_num);
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

然后我们运行程序:

lifecycle1

我们运行后,点击按钮,将num的值累加到103,

WX20210625-195946@2x

然后切换横竖屏,发现num的值又变回100了。

WX20210625-193642@2x

观察HiLog日志:

WX20210625-195849@2x

06-25 19:57:02.990 22192-22192/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 19:57:03.008 22192-22192/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
06-25 19:58:15.003 22192-22192/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onInactive()
06-25 19:58:15.146 22192-22192/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onBackground()
06-25 19:58:15.148 22192-22192/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStop()
06-25 19:58:15.298 22192-22192/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 19:58:15.382 22192-22192/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我们发现,依次执行了onInactive()–>onBackground()–>onStop(),然后会重新执行onStart(),onActive()方法,所以切换到横屏后,num的数值又变回了100。

那如果想保留num的数值,还要再借助于两个生命周期函数:onSaveAbilityState()和onRestoreAbilityState():

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
        HiLog.info(LABEL, "==MainAbility--->onSaveAbilityState()");

        outState.putIntValue("num", num);
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        HiLog.info(LABEL, "==MainAbility--->onRestoreAbilityState()");
        num = inState.getIntValue("num");
        textNum.setText("血量num:"+num);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

我们重新运行一下:

lifecycle2

我们再来观察一下生命周期函数:

WX20210625-200518@2x

06-25 20:03:33.150 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 20:03:33.171 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
06-25 20:03:57.537 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onInactive()
06-25 20:03:57.584 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onBackground()
06-25 20:03:57.585 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onSaveAbilityState()
06-25 20:03:57.586 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStop()
06-25 20:03:57.761 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onStart()
06-25 20:03:57.764 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onRestoreAbilityState()
06-25 20:03:57.770 9044-9044/com.example.hanrupageabilitylifecycle I 00201/TAG_ABILITY: ==MainAbility--->onActive()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

最终切换到横屏之后,num也是103,完美。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/252671
推荐阅读
相关标签