Transfer: https://www.jianshu.com/p/49c153fca075
I encountered a problem writing a business logic. WXML data in the small program is not exit when you return to wechat home page, only onhide, and there is no small program to exit the small program to monitor the semaphore, it can be implemented, but if the small program has tabbar, this method also does not work. Therefore, the problem I encountered was that the first time I scanned the code into a small program, refreshed the data, returned to the main interface of wechat, and then opened the small program, the data of the home page in the case of no new request, the previously bound data is gone; However,, if you manually refresh the data and still return to the main page of wechat, and then open this small program, without refreshing the data, the last data will be retained, which makes me very upset. No data small procedures. It will probably be deleted in seconds. Then I looked at the WXML lifecycle for an answer
I found that… In the case of not killing wechat app.
- For the first time to open the small program, the first three methods above should be executed once. When returning to the main page of wechat for the first time, onHide and onUnload should be executed once.
- When you open the mini program for the second time, each of the above three methods will be executed once. When you return to the main page of wechat for the second time, onHide will be executed once and onUnload will not be executed.
- Open the small program for the third time, onLoad, onReady not executed, onShow executed once; When the user returns to the main page of wechat for the third time, onHide is executed once and onUnload is not executed.
Open the small program for the fourth time, the same as the third time
The fifth time… The same as the third…
.
That is, when the applet performs onUnload, the WXML bound data disappears and then the applet uses the onLoad method
If you look carefully, you can see the difference between the first exit and the second exit:
The first time you open the applet and exit is on the following page
The second time you open the applet and exit is the following page
When you open an applet in Figure 3, you use the onLoad method because the last time you closed it it used the onUnload method. When you open an applet in Figure 4, you don’t use the onLoad method because the last time you unload it it didn’t use the onUnload method. The life cycle of the applet has changed a lot. Of course, most of the business logic of this detail does not need to be concerned, but when it is really used, not clear about this knowledge point may write a lot of code, I believe you will appreciate me at some point, haha
Tips: All cases are discussed in the case that the wechat app is not killed. If the wechat app is killed, the small program will be killed naturally