自定义组件的生命周期(推荐)
自定义组件的生命周期回调函数用于通知用户该自定义组件的生命周期,这些回调函数是私有的,在运行时由开发框架在特定的时间进行调用,不能从应用程序中手动调用这些回调函数。
说明:
- 本模块首批接口从API version 23开始支持,后续版本的新增接口,采用上角标单独标记接口的起始版本。
@ComponentInit
ComponentInit: MethodDecorator
@ComponentInit装饰的函数在自定义组件初始化即将完成时执行。开发者可以在此时注册监听和修改变量。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
示例:
参见生命周期使用示例。
@ComponentAppear
ComponentAppear: MethodDecorator
与aboutToAppear相似,@ComponentAppear装饰的函数在创建自定义组件的新实例后,在其build()函数执行前调用,不同的是,@ComponentAppear装饰的函数仅在自定义组件处于CustomComponentLifecycleState.INIT状态才会触发。允许在@ComponentAppear装饰的函数中改变状态变量,更改将在后续执行build()函数中生效。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
示例:
参见生命周期使用示例。
@ComponentBuilt
ComponentBuilt: MethodDecorator
@ComponentBuilt装饰的函数在自定义组件的build()函数首次执行后调用,即从CustomComponentLifecycleState.APPEARED到CustomComponentLifecycleState.BUILT的阶段触发。开发者可以在这个阶段实现埋点数据上报等不影响实际UI的功能。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
示例:
参见生命周期使用示例。
@ComponentDisappear
ComponentDisappear: MethodDecorator
@ComponentDisappear装饰的函数在自定义组件析构销毁时执行。不建议在此函数中改变状态变量,特别是@Link变量的修改可能会导致应用程序行为不稳定。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
示例:
参见生命周期使用示例。
@ComponentReuse
ComponentReuse: MethodDecorator
当可复用的自定义组件从缓存中重新添加到节点树时调用@ComponentReuse装饰的函数,即从CustomComponentLifecycleState.RECYCLED到CustomComponentLifecycleState.BUILT阶段触发,以接收组件的构造参数。最后,复用会递归遍历所有子组件,对每个完成复用的子组件,会调用子组件中@ComponentReuse装饰的函数。
说明:
在状态管理V1的组件里,@ComponentReuse装饰的函数允许有一个入参或者无参。入参params建议为Record<string, Object |undefined |null>类型。
在状态管理V2的组件里,@ComponentReuse装饰的函数没有入参。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| params | Record<string, Object |undefined |null> | 否 | 当params存在时,表示V1组件的复用回调。 |
示例:
参见生命周期使用示例。
@ComponentRecycle
ComponentRecycle: MethodDecorator
当组件被回收后触发,先执行应用程序中定义的必要回收操作,完成回收后调用此装饰器装饰的函数,即从CustomComponentLifecycleState.BUILT到CustomComponentLifecycleState.RECYCLED阶段触发。最后,回收会递归遍历所有子组件,对每个完成回收的子组件调用子组件中@ComponentRecycle装饰的函数。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
示例:
参见生命周期使用示例。
CustomComponentLifecycle
CustomComponentLifecycle用于监控自定义组件生命周期的变化,开发者可以通过UIUtils.getLifecycle获取CustomComponentLifecycle实例。
getCurrentState
getCurrentState(): CustomComponentLifecycleState
getCurrentState函数用于获得自定义组件当前的生命周期状态。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
返回值:
| 类型 | 说明 |
|---|---|
| CustomComponentLifecycleState | 自定义组件当前的生命周期状态。 |
示例:
import { UIUtils, ComponentBuilt } from '@kit.ArkUI';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@Component
struct Index {
@ComponentBuilt
myBuilt() {
// CustomComponentLifecycle.getCurrentState用于获得自定义组件当前的生命周期状态
hilog.info(0x0000, 'testTag', 'Index Lifecycle is %{public}d', UIUtils.getLifecycle(this).getCurrentState());
}
build() {
Column() {
Text(`HelloWorld`)
}
.height('100%')
.width('100%')
}
}
addObserver
addObserver(observer: CustomComponentLifecycleObserver): void
addObserver函数用于注册自定义组件生命周期监听器。当自定义组件的生命周期发生变化时,会触发监听器中相应的生命周期回调函数。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| observer | CustomComponentLifecycleObserver | 是 | 监听自定义组件的监听器。 |
removeObserver
removeObserver(observer: CustomComponentLifecycleObserver): void
removeObserver函数用于移除自定义组件生命周期监听器。解除注册后,即使自定义组件的生命周期状态发生变化,也不会触发监听器中相应的生命周期回调函数。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| observer | CustomComponentLifecycleObserver | 是 | 监听自定义组件的监听器。 |
CustomComponentLifecycleObserver
用户注册自定义组件生命周期回调后,当该自定义组件的生命周期发生变化时,将触发监听器中相应的生命周期回调。
aboutToAppear
aboutToAppear?(): void
aboutToAppear函数在创建自定义组件的新实例后,执行其build()函数之前执行。开发者可以在此阶段修改状态变量。其功能与aboutToAppear类似,但是在自定义组件状态机的约束下触发的。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
onDidBuild
onDidBuild?(): void
onDidBuild函数在自定义组件的新实例构建完成后,执行其build()函数之后执行。开发者可以在此阶段实现一些不影响实际UI的功能,例如事件数据上报。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
aboutToDisappear
aboutToDisappear?(): void
aboutToDisappear函数在自定义组件被销毁之前执行。不建议在aboutToDisappear函数中修改状态变量,特别是@Link变量的修改可能会导致应用程序行为不稳定。其功能与aboutToDisappear类似,不同的是,CustomComponentLifecycleObserver中的aboutToDisappear函数受状态机约束,只有被监听的自定义组件状态向CustomComponentLifecycleState.DISAPPEARED转变前触发回调。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
aboutToReuse
aboutToReuse?(params?: Record<string, Object|undefined|null>): void
当可复用的自定义组件从缓存中重新添加到节点树时调用aboutToReuse函数,以接收组件的构造参数。当params存在时,表示V1组件的复用回调。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| params | Record<string, Object |undefined |null> | 否 | 当params存在时,表示V1组件的复用回调。 |
aboutToRecycle
aboutToRecycle?(): void
当组件被回收后触发,先执行应用程序中定义的必要回收操作,完成回收后调用aboutToRecycle函数。随后该组件被冻结,以避免该组件处于回收池时进行UI更新。最后,aboutToRecycle函数会递归遍历所有子组件,对每个完成回收的组件调用aboutToRecycle函数。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
示例:
import { ComponentInit, ComponentDisappear, UIUtils, CustomComponentLifecycleObserver, CustomComponentLifecycle } from '@kit.ArkUI';
import { hilog } from '@kit.PerformanceAnalysisKit';
export class Message {
value: string|undefined;
constructor(value: string) {
this.value = value;
}
}
@Entry
@Component
struct Index {
@State switch: boolean = true;
build() {
Column() {
Button('Hello')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.switch = !this.switch;
})
if (this.switch) {
// 如果只有一个复用的组件,可以不用设置reuseId。
Child({ message: new Message('Child') })
.reuseId('Child')
}
}
.height('100%')
.width('100%')
}
}
@Reusable
@Component
struct Child {
@State message: Message = new Message('AboutToReuse');
@State label: string = 'HelloWorld';
@ComponentInit
myInit(): void {
registerObserver(UIUtils.getLifecycle(this));
}
@ComponentDisappear
myDisappear(): void {
unRegisterObserver(UIUtils.getLifecycle(this));
}
build() {
Column() {
Text(this.message.value)
.fontSize(30)
}
}
}
export class MyObserver implements CustomComponentLifecycleObserver {
// 重写CustomComponentLifecycleObserver中的生命周期事件。
aboutToAppear() {
hilog.info(0x0000, 'testTag', 'MyObserver aboutToAppear');
}
onDidBuild() {
hilog.info(0x0000, 'testTag', 'MyObserver onDidBuild');
}
aboutToReuse(params?: Record<string, Object|undefined|null>) {
// params存在时,为V1的复用;
hilog.info(0x0000, 'testTag', 'MyObserver aboutToReuse');
}
aboutToRecycle() {
hilog.info(0x0000, 'testTag', 'MyObserver aboutToRecycle');
}
aboutToDisappear() {
hilog.info(0x0000, 'testTag', 'MyObserver aboutToDisappear');
}
}
// 创建Observer对象
const observer = new MyObserver();
export function registerObserver(lifeCycle: CustomComponentLifecycle) {
// 向lifeCycle注册监听
lifeCycle.addObserver(observer);
}
export function unRegisterObserver(lifeCycle: CustomComponentLifecycle) {
// 向lifeCycle取消注册监听
lifeCycle.removeObserver(observer);
}
CustomComponentLifecycleState
自定义组件当前的生命周期状态。
原子化服务API: 从API version 23开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
模型约束: 此接口仅可在Stage模型下使用。
| 名称 | 值 | 说明 |
|---|---|---|
| INIT | 0 | 初始化状态。 |
| APPEARED | 1 | 准备展开状态。 |
| BUILT | 2 | 已展开状态。 |
| RECYCLED | 3 | 回收状态。 |
| DISAPPEARED | 4 | 删除状态。 |
示例:
import { CustomComponentLifecycleState, ComponentBuilt } from '@kit.ArkUI';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@Component
struct Index {
@ComponentBuilt
myBuilt() {
// CustomComponentLifecycleState.BUILT代表自定义组件为已展开状态
hilog.info(0x0000, 'testTag', 'Index Lifecycle is %{public}d', CustomComponentLifecycleState.BUILT);
}
build() {
Column() {
Text(`HelloWorld`)
}
.height('100%')
.width('100%')
}
}
生命周期使用示例
本示例展示了生命周期回调函数的部分使用场景:
-
自定义组件Child的创建触发@ComponentInit、@ComponentAppear,Child执行build()后,触发@ComponentBuilt。
-
更改this.switch为false,回收Child子组件触发@ComponentRecycle;更改this.switch为true,复用Child子组件触发@ComponentReuse。
-
退出应用,在自定义组件Child销毁前,触发@ComponentDisappear。
import { ComponentInit, ComponentAppear, ComponentBuilt, ComponentDisappear, ComponentReuse, ComponentRecycle } from '@kit.ArkUI';
import { hilog } from '@kit.PerformanceAnalysisKit';
export class Message {
value: string|undefined;
constructor(value: string) {
this.value = value;
}
}
@Entry
@Component
struct Index {
@State switch: boolean = true;
build() {
Column() {
Button('Hello')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.onClick(() => {
this.switch = !this.switch;
})
if (this.switch) {
// 如果只有一个复用的组件,可以不用设置reuseId。
Child({ message: new Message('Child') })
.reuseId('Child')
}
}
.height('100%')
.width('100%')
}
}
@Reusable
@Component
struct Child {
@State message: Message = new Message('Child');
@State label: string = 'HelloWorld';
@State switch: boolean = true;
@ComponentInit
myInit() {
// 自定义组件创建完毕后,触发myInit方法
hilog.info(0x0000, 'testTag', 'Child myInit');
}
@ComponentAppear
myAppear() {
this.label = 'myAppear'
hilog.info(0x0000, 'testTag', 'Child myAppear');
}
@ComponentBuilt
myBuilt() {
this.label = 'myBuilt'
hilog.info(0x0000, 'testTag', 'Child myBuilt');
}
@ComponentRecycle
myRecycle() {
this.label = 'myRecycle'
hilog.info(0x0000, 'testTag', 'Child myRecycle');
}
@ComponentDisappear
myDisappear() {
this.label = 'myDisappear'
hilog.info(0x0000, 'testTag', 'Child myDisappear');
}
@ComponentReuse
myReuse() {
this.label = 'myReuse'
hilog.info(0x0000, 'testTag', 'Child myReuse');
}
build() {
Column() {
Text(this.message.value)
.fontSize(30)
}
.borderWidth(1)
.height(100)
}
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 ts-drawing-components-shape
openharmony 鸿蒙 ts-universal-attributes-sharp-clipping
openharmony 鸿蒙 ts-universal-attributes-toolbar
openharmony 鸿蒙 ts-container-lazyvgridlayout
openharmony 鸿蒙 ohos-arkui-advanced-FoldSplitContainer
openharmony 鸿蒙 ts-methods-calendarpicker-dialog
openharmony 鸿蒙 ts-uicommonevent
openharmony 鸿蒙 ts-basic-gestures-tapgesture