当前位置:   article > 正文

HarmonyOS组件的生命周期_abouttoappear abouttodisappear onpagehide onpagesh

abouttoappear abouttodisappear onpagehide onpageshow

带Entry修饰的组件的生命周期

不带Entry修饰的组件的生命周期

组件的生命周期有以下5个方法
但是不带entry修饰的组件只有1和2方法

 1. aboutToAppear()
 2. aboutToDisappear()
 3. onPageShow()
 4. onBackPress()
 5. onPageHide()
  • 1
  • 2
  • 3
  • 4
  • 5

下面先大致说下这几个方法

aboutToAppear()

这个方法在组件被创建到build执行之前被系统调用
一般在这个方法里面做页面初始化数据

aboutToDisappear()

这个方法在组件被销毁的时候被系统调用,一般在这个方法里面回收资源

onPageShow()

页面显示的时候调用

onBackPress()

按下返回键被系统调用

onPageHide()

当前页面不可见的时候被系统调用

为了搞清楚带entry 修饰的组件页面从创建到销毁过程中的生命周期,已Index SecondPage 为例 在页面中重写上面5个方法。在方法被调用的时候打印下日志。

Index.ets

import hilog from '@ohos.hilog'
import router from '@ohos.router'
@Entry
@Component
struct Index {
  @State TAG: string = 'entry model'

  aboutToAppear(){
    hilog.info(0x000,this.TAG,'%{public}s','Index Page aboutToAppear')
  }

  aboutToDisappear(){
    hilog.info(0x000,this.TAG,'%{public}s','Index Page aboutToDisappear')
  }

  onPageShow(){
    hilog.info(0x000,this.TAG,'%{public}s','Index Page onPageShow')
  }

  onPageHide(){
    hilog.info(0x000,this.TAG,'%{public}s','Index Page onPageHide')
  }

  onBackPress(){
    hilog.info(0x000,this.TAG,'%{public}s','Index Page onBackPress')
  }

  build() {
    Row() {
      Column() {
        Text(this.TAG)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            router.pushUrl({
              url:'pages/SecondPage'
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 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

SecondPage.ets

import hilog from '@ohos.hilog'

@Entry
@Component
struct SecondPage {
  @State TAG: string = 'entry model'

  aboutToAppear() {
    hilog.info(0x000, this.TAG, '%{public}s',  'Second Page aboutToAppear')
  }

  aboutToDisappear() {
    hilog.info(0x000, this.TAG, '%{public}s',  'Second Page aboutToDisappear')
  }

  onPageShow() {
    hilog.info(0x000, this.TAG, '%{public}s',  'Second Page onPageShow')
  }

  onPageHide() {
    hilog.info(0x000, this.TAG, '%{public}s',  'Second Page onPageHide')
  }

  onBackPress() {
    hilog.info(0x000, this.TAG, '%{public}s',  'Second Page onBackPress')
  }

  build() {
    Row() {
      Column() {
        Text(this.TAG)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 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

运行项目,首先看到如下日志

01-25 13:54:11.852 21437-1284/? I 00000/entry model: Index Page aboutToAppear
01-25 13:54:11.869 21437-1284/? I 00000/entry model: Index Page onPageShow
  • 1
  • 2

日志分析: 打开第一个页面Index.ets ,执行aboutToAppear ,然后页面开始显示执行onPageShow方法。
点击Text 跳转到SecondPage

01-25 13:57:52.654 21437-1284/? I 00000/entry model: Second Page aboutToAppear
01-25 13:57:52.656 21437-1284/? I 00000/entry model: Index Page onPageHide
01-25 13:57:52.656 21437-1284/? I 00000/entry model: Second Page onPageShow
  • 1
  • 2
  • 3

日志分析: 打开第二个页面SecondPage.ets, 执行aboutToAppear, 然后第一个页面不可见执onPageHide, 第二个页面开始显示执行onPageShow方法。
点击返回按钮回到Index.ets页面

01-25 14:02:41.293 21437-1284/? I 00000/entry model: Second Page onBackPress
01-25 14:02:41.295 21437-1284/? I 00000/entry model: Second Page onPageHide
01-25 14:02:41.295 21437-1284/? I 00000/entry model: Index Page onPageShow
01-25 14:02:41.614 21437-1284/? I 00000/entry model: Second Page aboutToDisappear
  • 1
  • 2
  • 3
  • 4

日志分析: 点击返回按钮回到Index.ets页面,SecondPaget 执行返回方法onBackPress,回到Index.ets页面要显示SecondPage.ets 要先不可见所以执行onPageHide, 然后Index.ets开始显示执行onPageShow方法。
之后SecondPage.ets 被销毁执行aboutToDisappear。
点击Home按钮回到后台

01-25 14:06:45.317 21437-1284/? I 00000/entry model: Index Page onPageHide
  • 1

此时应用退到后台Index.ets 不可见执行onPageHide 方法

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

闽ICP备14008679号