May 17, 2021 WeChat Mini Program Development Document
After the small program starts, the interface is presented to the user, at which point the small program is in the foreline state.
When the user clicks the upper right-hand capsule button to close the small program, or press the device Home key to leave WeChat, the small program does not completely terminate the operation, but into the background state, the small program can also run for a short period of time.
When the user enters WeChat again or opens the small program again, the small program enters the fore desk from the background. However, if the user has not re-entered the small program for a long time, or the system resources are tight, the small program may be destroyed, that is, completely out of operation.
In this way, small program startup can be divided into two situations, one is cold start, the other is hot start.
Typically, a small program is destroyed only if it enters the background for a certain amount of time, or if the system resources are too high. Specifically, there are several scenarios:
Basic library 1.1.0 and above, 1.4.0 below: When the user enters the applet from the entrance to sweep, forwarding and other entrances (the scene value is 1007, 1011, 1025), and if there is no top apparent program,The applet will be destroyed.
When the user opens a small program, the scene can be divided into two categories: B.
A. Keep the last browsing status. Scene Have the following:
Scene value ID | illustrate |
---|---|
1001 | Discover the small program master entrance, "Recent Used" list (the foundation version 2.2.4 contains "My Small Program" list) |
1003 | Star standard small program list |
1023 | System desktop small icon Open a small program |
1038 | Returns a small program from other applets |
1056 | Chat top music player top right corner menu, open the small program |
1080 | Customer service session menu small program entrance, open the small program |
1083 | Public number session menu small program entrance, open the small program (only Tencent customer service small program has) |
1089 | Chat main interface pull-down, open small program / WeChat chat main interface pull-down, "Recent use" column (base library version 2.2.4 from the inclusion of "My small program" column) |
1090 | Press and hold the menu in the upper right corner of the program to open the small program |
1103 | Discover - Small program main entrance my small program, open small program |
1104 | Chat main interface pull down, from my small program, open the small program |
1113 | Android phone negative screen, open the small program |
1114 | Android phone sidebar, open the small program |
1117 | In the management page of running the small program in the background, open the small program |
B. Relaunch to the specified page or home page
Includes scenarios other than Class A
Base library 2.8.0 starts to support, and low versions need to be compatible.
After the program is destroyed, the next cold boot, if it belongs to a Class B scenario, will go to a specific page.
The next cold boot, if it belongs to a Class A scenario, will go to the home page of the small program by default. In the json file for the page (or in the window segment of app.json globally), specifying the restartStrategy configuration item can change the default behavior so that the next cold start of a Class A scenario can be returned to that page after exiting from a page.
Code example:
{
"restartStrategy": "homePage"
}
RestartStrategy optional value:
Optional values | Meaning |
---|---|
homePage | (Default) If you exit the program from this page, the next time you start cold from the home page |
homePageAndLatestPage | If you exit the program from this page and load the page immediately after the next cold start, the parameters of the page remain the same (not available for tab pages) |
Note: Even if it is not configured as homePage, if the small program exits too long (the current default time of day, you can use the exit state to adjust), the next cold start will no longer follow the configuration of restartStrategy, but will start directly from the home page cold.
In any case, the state on the page is not preserved, such as the text content in the input box, checkbox check box check state, etc. will not be restored. If you need to restore or partially restore, you need to take advantage of the exit state.
The page callback function onSaveExitState is called whenever a small program may be destroyed. If you want to preserve the state on the page, you can "save" some data in this callback function, which you can get the next time you start it through exitState.
Code example:
{
"restartStrategy": "homePageAndLatestPage"
}
Page({
onLoad: function() {
var prevExitState = this.exitState // 尝试获得上一次退出前 onSaveExitState 保存的数据
if (prevExitState !== undefined) { // 如果是根据 restartStrategy 配置进行的冷启动,就可以获取到
prevExitState.myDataField === 'myData'
}
},
onSaveExitState: function() {
var exitState = { myDataField: 'myData' } // 需要保存的数据
return {
data: exitState,
expireTimeStamp: Date.now() + 24 * 60 * 60 * 1000 // 超时时刻
}
}
})
The onSaveExitState return value can contain two items:
The field name | Type | Meaning |
---|---|---|
data | Any | Data to be saved (only JSON-compatible data) |
expireTimeStamp | Number | Time-out time, after which the saved data is guaranteed to be discarded, defaulting to (current moment plus 1 day) |
Note: