This content originally appeared on DEV Community and was authored by wei chang
Good morning, everyone. Today’s topic is about the life cycle in Cangjie’s language development.
In fact, You LAN Jun prefers to share practical code writing and doesn’t like to talk too much about theoretical things. The reason why I wrote a separate article today to share the content of the life cycle is that if this part is not covered, it’s really easy to fall into a trap.
The life cycle refers to the process from loading, displaying to disappearing of a page or component. During this process, there will be some system events, and we can perform some operations in it. For instance, in ArkTs, there are methods such as aboutToAppear() and onPageShow(). So, what is the life cycle like in Cangjie?
If you have no knowledge of Cangjie and can only try to write it in the ArkTs way, you will find that there is no code prompt for this part in Cangjie. Then you have to force yourself to write down methods like “aboutToAppear” and “onPageShow” completely by hand, like this:
func aboutToAppear(){
AppLog.info("tabbar-aboutToAppear")
}
func onPageShow(){
AppLog.info("tabbar-aboutToAppear")
}
Run it and you will find that it is not only ineffective but also reports errors. Isn’t it very frustrating?
The life cycle of Cangjie is indeed these few, but you would never imagine how it is written. It is to add the two prefix modifiers “protected” and “open” in the above method:
protected open func aboutToAppear(){
AppLog.info("tabbar-aboutToAppear")
}
protected open func aboutToDisappear(){
AppLog.info("tabbar-aboutToDisappear")
}
protected open func onPageShow(){
AppLog.info("tabbar-onPageShow")
}
protected open func onPageHide(){
AppLog.info("tabbar-onPageHide")
}
protected open func onBackPress(){
AppLog.info("tabbar-onBackPress")
return true
}
In the component or page decorated with @Entry, there are several lifecycle functions such as aboutToAppear, aboutToDisappear, onPageShow, onPageHide, and onBackPress. The timing of their execution can be guessed by their names. Among them, onBackPress has a bool type return value. When it returns true, it indicates that the system’s return method is not used and the return logic is handled by itself. Conversely, it returns false.
In components modified only by @Component, there are only two lifecycle functions, aboutToAppear and aboutToDisappear, which is consistent with Arkts.
The above is all the relevant content about the life cycle of Cangjie language. #HarmonyOS Language ## Cangjie ## Shopping #
This content originally appeared on DEV Community and was authored by wei chang