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. 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. 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## aboutToDisappear 30 31aboutToDisappear?(): void 32 33Invoked before the destructor of the custom component is consumed. 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. 34 35**Widget capability**: This API can be used in ArkTS widgets since API version 9. 36 37**Atomic service API**: This API can be used in atomic services since API version 11. 38 39**System capability**: SystemCapability.ArkUI.ArkUI.Full 40 41## onPageShow 42 43onPageShow?(): void 44 45Invoked 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**. 46 47**Atomic service API**: This API can be used in atomic services since API version 11. 48 49**System capability**: SystemCapability.ArkUI.ArkUI.Full 50 51## onPageHide 52 53onPageHide?(): void 54 55Invoked 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**. 56 57**Atomic service API**: This API can be used in atomic services since API version 11. 58 59**System capability**: SystemCapability.ArkUI.ArkUI.Full 60 61## onBackPress 62 63onBackPress?(): void | boolean 64 65Invoked 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. 66 67**Atomic service API**: This API can be used in atomic services since API version 11. 68 69**System capability**: SystemCapability.ArkUI.ArkUI.Full 70 71```ts 72// xxx.ets 73@Entry 74@Component 75struct IndexComponent { 76 @State textColor: Color = Color.Black; 77 78 onPageShow() { 79 this.textColor = Color.Blue; 80 console.info('IndexComponent onPageShow'); 81 } 82 83 onPageHide() { 84 this.textColor = Color.Transparent; 85 console.info('IndexComponent onPageHide'); 86 } 87 88 onBackPress() { 89 this.textColor = Color.Red; 90 console.info('IndexComponent onBackPress'); 91 } 92 93 build() { 94 Column() { 95 Text('Hello World') 96 .fontColor(this.textColor) 97 .fontSize(30) 98 .margin(30) 99 }.width('100%') 100 } 101} 102``` 103 104 105## aboutToReuse<sup>10+</sup> 106 107aboutToReuse?(params: { [key: string]: unknown }): void 108 109Invoked when a reusable custom component is re-added to the node tree from the reuse cache to receive construction parameters of the component. 110 111**Atomic service API**: This API can be used in atomic services since API version 11. 112 113**System capability**: SystemCapability.ArkUI.ArkUI.Full 114 115**Parameters** 116 117| Name | Type | Description | 118|--------|----------------------------|------------| 119| params | { [key: string]: unknown } | Construction parameters of the custom component.| 120 121```ts 122// xxx.ets 123export class Message { 124 value: string | undefined; 125 126 constructor(value: string) { 127 this.value = value 128 } 129} 130 131@Entry 132@Component 133struct Index { 134 @State switch: boolean = true 135 136 build() { 137 Column() { 138 Button('Hello World') 139 .fontSize(50) 140 .fontWeight(FontWeight.Bold) 141 .onClick(() => { 142 this.switch = !this.switch 143 }) 144 if (this.switch) { 145 Child({ message: new Message('Child') }) 146 } 147 } 148 .height("100%") 149 .width('100%') 150 } 151} 152 153@Reusable 154@Component 155struct Child { 156 @State message: Message = new Message('AboutToReuse'); 157 158 aboutToReuse(params: Record<string, ESObject>) { 159 console.info("Recycle Child") 160 this.message = params.message as Message 161 } 162 163 build() { 164 Column() { 165 Text(this.message.value) 166 .fontSize(20) 167 } 168 .borderWidth(2) 169 .height(100) 170 } 171} 172``` 173 174## onWillApplyTheme<sup>12+</sup> 175 176onWillApplyTheme?(theme: Theme): void 177 178Invoked 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. 179 180**Atomic service API**: This API can be used in atomic services since API version 12. 181 182**System capability**: SystemCapability.ArkUI.ArkUI.Full 183 184**Parameters** 185 186| Name | Type | Description | 187|--------|------------------------------------------|------------| 188| theme | [Theme](../js-apis-arkui-theme.md#theme) | Current theme object of the custom component.| 189 190```ts 191// xxx.ets 192import { CustomTheme, CustomColors, Theme, ThemeControl } from '@kit.ArkUI'; 193 194class BlueColors implements CustomColors { 195 fontPrimary = Color.White; 196 backgroundPrimary = Color.Blue; 197 brand = Color.Blue; // Brand color 198} 199 200class PageCustomTheme implements CustomTheme { 201 colors?: CustomColors; 202 203 constructor(colors: CustomColors) { 204 this.colors = colors; 205 } 206} 207const BlueColorsTheme = new PageCustomTheme(new BlueColors()); 208// setDefaultTheme should be called on the application entry page or in an ability. 209ThemeControl.setDefaultTheme(BlueColorsTheme); 210 211@Entry 212@Component 213struct IndexComponent { 214 @State textColor: ResourceColor = $r('sys.color.font_primary'); 215 @State columBgColor: ResourceColor = $r('sys.color.background_primary'); 216 217 // 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. 218 onWillApplyTheme(theme: Theme) { 219 this.textColor = theme.colors.fontPrimary; 220 this.columBgColor = theme.colors.backgroundPrimary; 221 console.info('IndexComponent onWillApplyTheme'); 222 } 223 224 build() { 225 Column() { 226 // Initial color style of the component 227 Column() { 228 Text('Hello World') 229 .fontColor($r('sys.color.font_primary')) 230 .fontSize(30) 231 } 232 .width('100%') 233 .height('25%') 234 .borderRadius('10vp') 235 .backgroundColor($r('sys.color.background_primary')) 236 237 // The color style configured in onWillApplyTheme is applied. 238 Column() { 239 Text('onWillApplyTheme') 240 .fontColor(this.textColor) 241 .fontSize(30) 242 Text('Hello World') 243 .fontColor(this.textColor) 244 .fontSize(30) 245 } 246 .width('100%') 247 .height('25%') 248 .borderRadius('10vp') 249 .backgroundColor(this.columBgColor) 250 } 251 .padding('16vp') 252 .backgroundColor('#dcdcdc') 253 .width('100%') 254 .height('100%') 255 } 256} 257``` 258 259