当前位置:网站首页>The lifecycle of arkui development framework components
The lifecycle of arkui development framework components
2022-07-27 09:08:00 【Hua Weiyun】
ArkUI The development framework gives components a unique lifecycle approach , Here are two main situations :
System component life cycle
Customize the lifecycle of the component
System component life cycle
onAppear and onDisAppear ,
onAppear: Callback of the component mounted from the component tree .
onDisAppear: Components unloaded from the component tree return to .
A simple example is shown below :
@Entry @Component struct Index { @State textShow: boolean = false; // Default state build() { Column() { Column() { if (this.textShow) { Text(' mount / uninstall ') .fontSize(22) .onAppear(() => { console.log(" ha-ha , I'm attached ") }) .onDisAppear(() => { console.log(" Sobbing , I was uninstalled ") }) } } .width('100%') .height(60) Button(this.textShow ? " uninstall " : " mount ") .stateStyles({ pressed: { .backgroundColor(Color.Pink) // Set the style when clicking } }) .onClick(() => { // Mount and unload in turn Text Components this.textShow = !this.textShow; }) } .width('100%') .height('100%') }}Customize the lifecycle of the component
Customize the lifecycle of the component
Use
@ComponentDecorated components ,ArkUI The development framework will automatically give it a private lifecycle approachaboutToAppear()andaboutToDisappear(), They are used to inform developers of changes to the lifecycle of the custom component .aboutToAppear: Function after creating a new instance of the custom component , In the execution of its
build()Function before execution . It is allowed to change the state variable in this function , The changes will be implemented laterbuild()Function .aboutToDisappear: The function executes before the custom component destructs and consumes . Changing state variables in this function is not allowed , especially
@LinkModification of variables can lead to unstable application behavior .
The life cycle of a page
A page is essentially a component , It's just that the page has an additional modifier for components
@Entry, This modifier indicates that the current component is a page , It needs to be inconfig.jsonMake configuration in , In addition to the life cycle of components, pages , It also has its own unique lifecycle approach :onPageShow: Trigger once when the page is displayed , Including routing process 、 The application enters the front and back stage and other scenes , only
@EntryModified custom components take effect .onPageHide: Trigger once when the page disappears , Including routing process 、 The application enters the front and back stage and other scenes , only
@EntryModified custom components take effect .onBackPress: Triggered when the user clicks the back button , only @Entry Modified custom components take effect . This method returns boolean Type value , The explanation is as follows :
return
trueIt means that the page handles the return logic by itself , No page routing .return
falseIndicates that the default return logic is used .No return value will be used as
falseHandle .
The comparison of component life cycle production tables is as follows :
| Function name | describe |
|---|---|
| onAppear | Methods unique to system components , Callback of the component mounted from the component tree . |
| onDisAppear | Methods unique to system components , Components unloaded from the component tree return to . |
| aboutToAppear | Function after creating a new instance of the custom component , In the execution of its build() Function before execution . It is allowed to change the state variable in this function , The changes will be implemented later build() Function . |
| aboutToDisappear | The function executes before the custom component destructs and consumes . Changing state variables in this function is not allowed , especially @Link Modification of variables can lead to unstable application behavior . |
| onPageShow | This callback is triggered when the page is displayed , Including routing process 、 The application enters the front and back stage and other scenes . only @Entry Modified custom components take effect . |
| onPageHide | This callback is triggered when the page disappears , Including routing process 、 The application enters the front and back stage and other scenes . only @Entry Modified custom components take effect . |
| onBackPress | Triggered when the user clicks the back button , This method returns boolean type ,true: It means that the page handles the return logic by itself , No page routing .false: Indicates that the default return logic is used . No return value will be used as false Handle . only @Entry Modified custom components take effect . |
One thing to watch out for ,
It is allowed to use Promise And asynchronous callback functions , For example, access to network resources , Timer setting, etc ;
It is not allowed to use async await .
For example, we have such a demand , When returning to the current page , Something needs to be done . Here is Flutter Medium .then, I look at the api,ArkUI The framework did not find a similar implementation ,

Is it in ArkUI There is no way in the framework ? Actually, you can try this !
// Life cycle The first page onPageShow(){ console.info(' Refresh the page ...............') }Did you finish the corresponding implementation skillfully ?
Daily arch , come on. !
Reference documents
边栏推荐
- Linear list (sequential storage, chain storage) (linked list of leading nodes, linked list of non leading nodes)
- PVT's spatial reduction attention (SRA)
- Is the operation of assigning values to int variables atomic?
- Detailed explanation of two methods of Sqlalchemy
- MySQL basic knowledge learning (I)
- 拍卖行做VC,第一次出手就投了个Web3
- 1.3.1 Full Permutation Problem
- Activation functions commonly used in deep learning
- Programming style
- Sequential storage and chain storage of stack implementation
猜你喜欢

flex布局 (实战小米官网)

Pyqt5 rapid development and practice 4.1 qmainwindow

音乐体验天花板!14个网易云音乐的情感化设计细节

500报错

【进程间通信IPC】- 信号量的学习

CUDA Programming -03: thread level

CUDA programming-01: build CUDA Programming Environment

苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击

Deep understanding of Kalman filter (2): one dimensional Kalman filter

500 error reporting
随机推荐
linux下安装oracle,本地PL/SQL连接Linux下的oracle导入表并新建用户和密码
Some practical, commonly used and increasingly efficient kubernetes aliases
qt中使用sqlite同时打开多个数据库文件
Full Permutation (depth first, permutation tree)
HUAWEI 机试题:火星文计算 js
【进程间通信IPC】- 信号量的学习
1.3.1 Full Permutation Problem
npm install报错 强制安装
pollFirst(),pollLast(),peekFirst(),peekLast()
拍卖行做VC,第一次出手就投了个Web3
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
Ztree custom title attribute
flex:1的原理
Activation functions commonly used in deep learning
TensorFlow损失函数
Mangodb简单使用
巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
Network IO summary
07_ Service registration and discovery summary
The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?