1# Custom Component Lifecycle 2 3The lifecycle callbacks of a custom component are used to notify users of the lifecycle of the component. These callbacks are private and are invoked by the development framework at a specified time at runtime. They cannot be manually invoked from applications. Do not reuse the same custom component node across multiple windows, as otherwise its lifecycle may become disrupted. 4 5>**NOTE** 6> 7>- The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8>- Promise and asynchronous callback functions can be used in lifecycle functions, for example, network resource getters and timer setters. 9 10 11## aboutToAppear 12 13aboutToAppear?(): void 14 15Invoked after a new instance of the custom component is created and before its **build()** function is executed. You can change state variables in **aboutToAppear**. The change will take effect when you execute the **build()** function next time. The **aboutToAppear** lifecycle callback of a custom component with a custom layout is invoked during the layout process. 16 17**Widget capability**: This API can be used in ArkTS widgets since API version 9. 18 19**Atomic service API**: This API can be used in atomic services since API version 11. 20 21**System capability**: SystemCapability.ArkUI.ArkUI.Full 22 23## onDidBuild<sup>12+</sup> 24 25onDidBuild?(): void 26 27Invoked after the **build()** function of the custom component is executed. Do not change state variables or use functions (such as **animateTo**) in **onDidBuild**. Otherwise, unstable UI performance may result. 28 29**Atomic service API**: This API can be used in atomic services since API version 12. 30 31## aboutToDisappear 32 33aboutToDisappear?(): void 34 35Invoked when this component is about to disappear. Do not change state variables in the **aboutToDisappear** function as doing this can cause unexpected errors. For example, the modification of the **@Link** decorated variable may cause unstable application running. 36 37**Widget capability**: This API can be used in ArkTS widgets since API version 9. 38 39**Atomic service API**: This API can be used in atomic services since API version 11. 40 41**System capability**: SystemCapability.ArkUI.ArkUI.Full 42 43## onPageShow 44 45onPageShow?(): void 46 47Invoked each time the page is displayed, for example, during page redirection or when the application is switched to the foreground. It works only for the custom components decorated by **@Entry**. 48 49**Atomic service API**: This API can be used in atomic services since API version 11. 50 51**System capability**: SystemCapability.ArkUI.ArkUI.Full 52 53## onPageHide 54 55onPageHide?(): void 56 57Invoked each time the page is hidden, for example, during page redirection or when the application is switched to the background. It works only for the custom components decorated by **@Entry**. 58 59**Atomic service API**: This API can be used in atomic services since API version 11. 60 61**System capability**: SystemCapability.ArkUI.ArkUI.Full 62 63## onBackPress 64 65onBackPress?(): void | boolean 66 67Invoked when the user clicks the Back button. It works only for the custom components decorated by @Entry. The value **true** means that the page executes its own return logic, and **false** (default) means that the default return logic is used. 68 69**Atomic service API**: This API can be used in atomic services since API version 11. 70 71**System capability**: SystemCapability.ArkUI.ArkUI.Full 72 73```ts 74// xxx.ets 75@Entry 76@Component 77struct IndexComponent { 78 @State textColor: Color = Color.Black; 79 80 onPageShow() { 81 this.textColor = Color.Blue; 82 console.info('IndexComponent onPageShow'); 83 } 84 85 onPageHide() { 86 this.textColor = Color.Transparent; 87 console.info('IndexComponent onPageHide'); 88 } 89 90 onBackPress() { 91 this.textColor = Color.Red; 92 console.info('IndexComponent onBackPress'); 93 } 94 95 build() { 96 Column() { 97 Text('Hello World') 98 .fontColor(this.textColor) 99 .fontSize(30) 100 .margin(30) 101 }.width('100%') 102 } 103} 104``` 105 106 107## aboutToReuse<sup>10+</sup> 108 109aboutToReuse?(params: { [key: string]: unknown }): void 110 111Invoked when a reusable custom component is re-added to the node tree from the reuse cache to receive construction parameters of the component. 112 113**Atomic service API**: This API can be used in atomic services since API version 11. 114 115**System capability**: SystemCapability.ArkUI.ArkUI.Full 116 117**Parameters** 118 119| Name | Type | Description | 120|--------|----------------------------|------------| 121| params | { [key: string]: unknown } | Construction parameters of the custom component.| 122 123```ts 124// xxx.ets 125export class Message { 126 value: string | undefined; 127 128 constructor(value: string) { 129 this.value = value 130 } 131} 132 133@Entry 134@Component 135struct Index { 136 @State switch: boolean = true 137 138 build() { 139 Column() { 140 Button('Hello World') 141 .fontSize(50) 142 .fontWeight(FontWeight.Bold) 143 .onClick(() => { 144 this.switch = !this.switch 145 }) 146 if (this.switch) { 147 Child({ message: new Message('Child') }) 148 } 149 } 150 .height("100%") 151 .width('100%') 152 } 153} 154 155@Reusable 156@Component 157struct Child { 158 @State message: Message = new Message('AboutToReuse'); 159 160 aboutToReuse(params: Record<string, ESObject>) { 161 console.info("Recycle Child") 162 this.message = params.message as Message 163 } 164 165 build() { 166 Column() { 167 Text(this.message.value) 168 .fontSize(20) 169 } 170 .borderWidth(2) 171 .height(100) 172 } 173} 174``` 175 176## aboutToRecycle<sup>10+</sup> 177 178aboutToRecycle?(): void 179 180Invoked when this reusable component is about to be added from the component tree to the reuse cache. 181 182**Atomic service API**: This API can be used in atomic services since API version 11. 183 184**System capability**: SystemCapability.ArkUI.ArkUI.Full 185 186```ts 187// xxx.ets 188export class Message { 189 value: string | undefined; 190 191 constructor(value: string) { 192 this.value = value; 193 } 194} 195 196@Entry 197@Component 198struct Index { 199 @State switch: boolean = true; 200 201 build() { 202 Column() { 203 Button('Hello World') 204 .fontSize(50) 205 .fontWeight(FontWeight.Bold) 206 .onClick(() => { 207 this.switch = !this.switch; 208 }) 209 if (this.switch) { 210 Child({ message: new Message('Child') }) 211 } 212 } 213 .height("100%") 214 .width('100%') 215 } 216} 217 218@Reusable 219@Component 220struct Child { 221 @State message: Message = new Message('AboutToReuse'); 222 223 aboutToReuse(params: Record<string, ESObject>) { 224 console.info("Reuse Child"); 225 this.message = params.message as Message; 226 } 227 228 aboutToRecycle() { 229 // This is where you can release memory-intensive content or other non-essential resource references to avoid continuous memory usage that could lead to memory leaks. 230 console.info("The child enters the recycle pool."); 231 } 232 233 build() { 234 Column() { 235 Text(this.message.value) 236 .fontSize(20) 237 } 238 .borderWidth(2) 239 .height(100) 240 } 241} 242``` 243 244## onWillApplyTheme<sup>12+</sup> 245 246onWillApplyTheme?(theme: Theme): void 247 248Invoked before the **build()** function of a new instance of the custom component is executed, to obtain the **Theme** object of the component context. You can change state variables in **onWillApplyTheme**. The change will take effect when you execute the **build()** function next time. 249 250**Atomic service API**: This API can be used in atomic services since API version 12. 251 252**System capability**: SystemCapability.ArkUI.ArkUI.Full 253 254**Parameters** 255 256| Name | Type | Description | 257|--------|------------------------------------------|------------| 258| theme | [Theme](../js-apis-arkui-theme.md#theme) | Current theme object of the custom component.| 259 260```ts 261// xxx.ets 262import { CustomTheme, CustomColors, Theme, ThemeControl } from '@kit.ArkUI'; 263 264class BlueColors implements CustomColors { 265 fontPrimary = Color.White; 266 backgroundPrimary = Color.Blue; 267 brand = Color.Blue; // Brand color 268} 269 270class PageCustomTheme implements CustomTheme { 271 colors?: CustomColors; 272 273 constructor(colors: CustomColors) { 274 this.colors = colors; 275 } 276} 277const BlueColorsTheme = new PageCustomTheme(new BlueColors()); 278// setDefaultTheme should be called on the application entry page or in an ability. 279ThemeControl.setDefaultTheme(BlueColorsTheme); 280 281@Entry 282@Component 283struct IndexComponent { 284 @State textColor: ResourceColor = $r('sys.color.font_primary'); 285 @State columBgColor: ResourceColor = $r('sys.color.background_primary'); 286 287 // Obtain the Theme object of the current component context. Set textColor and columBgColor in onWillApplyTheme to the color (BlueColorsTheme) of the Theme object in use. 288 onWillApplyTheme(theme: Theme) { 289 this.textColor = theme.colors.fontPrimary; 290 this.columBgColor = theme.colors.backgroundPrimary; 291 console.info('IndexComponent onWillApplyTheme'); 292 } 293 294 build() { 295 Column() { 296 // Initial color style of the component 297 Column() { 298 Text('Hello World') 299 .fontColor($r('sys.color.font_primary')) 300 .fontSize(30) 301 } 302 .width('100%') 303 .height('25%') 304 .borderRadius('10vp') 305 .backgroundColor($r('sys.color.background_primary')) 306 307 // The color style configured in onWillApplyTheme is applied. 308 Column() { 309 Text('onWillApplyTheme') 310 .fontColor(this.textColor) 311 .fontSize(30) 312 Text('Hello World') 313 .fontColor(this.textColor) 314 .fontSize(30) 315 } 316 .width('100%') 317 .height('25%') 318 .borderRadius('10vp') 319 .backgroundColor(this.columBgColor) 320 } 321 .padding('16vp') 322 .backgroundColor('#dcdcdc') 323 .width('100%') 324 .height('100%') 325 } 326} 327``` 328 329