# Weixin Mini Program Operation mechanism
# 1. Life cycle of Weixin Mini Program
Weixin Mini Program From startup to final destruction, it will go through many different states, and Mini Programs will behave differently in different states.
# 1.1 Weixin Mini Program Start
From a user awareness perspective, the general Weixin Mini Program startup can be divided into two situations, one is the cold startup and the other is the hot startup .
- Cold start: If the user opens it for the first time, or is opened again by the user after the destruction of Weixin Mini Program, the Mini Program needs to be reloaded to start, that is, cold start.
- Hot startup: If the user has already opened a Weixin Mini Program Mini Program and then opens the Mini Program again within a certain amount of time, the Mini Program is not destroyed, but simply goes from the background state to the front state, this process is hot startup.
From the perspective of the Weixin Mini Program lifecycle, what we generally refer to as startup refers specifically to cold startup, while hot startup is generally referred to as the back-office cut-front.
# 1.2 Front Desk and Back Desk
Weixin Mini Program After starting, the interface is presented to the user, and the Mini Program is in the " foreground " state.
When the user "turns off" Weixin Mini Program, the Mini Program is not really shut down, but goes into the background state, where the Mini Program can run for a short period of time, but some API usage is limited.Ways to cut backend include, but are not limited to, the following:
- Click the capsule button in the upper right corner to leave Weixin Mini Program
- IOS Swipe right away from the left side of the screen Weixin Mini Program
- Android Click Back to leave Weixin Mini Program
- Weixin Mini Program WeChat directly to the background (gesture or Home key)
- Weixin Mini Program Foreground runtime direct lock screen
When the user re-enters WeChat and opens Weixin Mini Program, the Mini Program will re-enter the " **foreground **" state.
# 1.3 Hang it up.
After a while in the background state (currently 5 seconds), WeChat stops the execution of the Mini Program's JS thread and the Mini Program enters the " hanging up the " state. The memory state of the Mini Program is retained at this time, but the developer code execution stops, and events and interface callbacks are triggered when the Mini Program enters the front desk again.
When the developer uses background musicWhen and back-end geolocations are capabilities, Weixin Mini Program can continue to run in the back-end without going into a "hanging up" state.
# 1.4 Weixin Mini Program Destruction
If the user has not used Weixin Mini Program for a long time, or if the system resources are tight, the Mini Program will be "destroyed by **** ," i.e. completely shut down.Specifically, this includes the following situations:
- When Weixin Mini Program enters the background and is "suspended," the Mini Program is destroyed if it does not enter the foreground again for a long time (currently 30 minutes).
- When Weixin Mini Program takes up too much system resources, it may be destroyed by the system or recovered by WeChat.
- On iOS, when a WeChat client receives a system memory alert for a certain period of time, it actively destroys Weixin Mini Program according to a strategy and prompts the user, "Running low on memory, please reopen the Mini Program."The specific strategy will be continuously adjusted and optimized.
- It is recommended that Weixin Mini Program use wx.onMemoryWarning to listen for memory alarm events when necessary and perform necessary internal cleanup.
Base Library 1.1.0 and above, 1.4.0 The following versions: When the user scans,Post an entry ( scenario value is 1007, 1008, 1011, 1025) into Weixin Mini Program-0]] and exit without a topless Mini Program. The Mini Program is destroyed.
# 2. Weixin Mini Program Cold Start Page
Weixin Mini Program On a cold boot, the open page has the following
- (Class A scenario) If there is no path in the starting scenario
- Base Library 2.8.0 For the following versions, go to the homepage
- Base library 2.8.0 and above follows the Restart Policy, which may be the home page or the page from which you last exited
- (Class B scenario) If the scenario starts with a path, start the page that enters the corresponding path
# 2.1 Reboot strategy
Start from base library version 2.8.0. Please remaining backward compatible.
Weixin Mini Program On a cold boot, if you start without a path (Class A scenario), you will enter the home page of the Mini Program by default.
In the json file corresponding to the page (or globally configured in the window section of apagejson),SpecifyingrestartStrategyconfiguration items can change this default behavior so that the next cold start of a class A scene can be returned to a page after exiting from it.
Code example:
{
"restartStrategy": "homePage"
}
RestartStrategyOptional values:
| Optional values | meaning |
|---|---|
| homePage | (Default) If you exit Weixin Mini Program from this page, the next time it will start cold from the home page |
| homePageAndLatestPage | If you exit Weixin Mini Program from this page, the page is loaded immediately after the next cold boot, and the parameters of the page are unchanged (not available for tab pages) |
Note: Even if you are not configured tohomePage, Weixin Mini Program if you quit for too long (the current default is one day,You can use exit status to adjust, and the next time you coldboot, you will no longer follow therestartStrategyconfiguration, but instead coldboot directly from the home page.
In any case, the state of the page is not preserved, such as the text content of the input box, the checkbox, etc., will not be restored.If a restore or partial restore is required, exit the state** with **.
# 3. Weixin Mini Program Hot Start Page
Weixin Mini Program When hot booting, the open page has the following
- (Class A scenario) If the starting scenario does not have a path, the state of the last browsing is retained
- (Class B scenario) If you start a scenario with a path, reLaunch to the page corresponding to the path
The following scenario values are common in Class A scenarios:
| Scene Value ID | Introductions |
|---|---|
| 1001 | Find bar Weixin Mini Program Main entrance, "Recent Use" list (Base library version 2.2.4 includes "My Mini Programs" list) |
| 1003 | List of stars Weixin Mini Program |
| 1023 | System desktop icon opens Weixin Mini Program |
| 1038 | Return Mini Program from other Weixin Mini Program |
| 1056 | At the top of the chat, open the Weixin Mini Program |
| 1080 | Customer service session menu Weixin Mini Program entry, open Mini Program |
| 1083 | Official Account session menu Weixin Mini Program entrance, open the Mini Program (only Tencent customer service Mini Program has) |
| 1089 | Chat home drop-down, open Weixin Mini Program / WeChat Chat home drop down, "Recent Use" column (from the base library version 2.2.4, the "My Preferences" column is included) |
| 1090 | Long press the Weixin Mini Program top right corner menu, open the Mini Program |
| 1103 | Found - Weixin Mini Program main entrance my Mini Program, open Mini Program |
| 1104 | Chat main interface drop-down, from my Weixin Mini Program, open the Mini Program |
| 1113 | Android phone negative screen, open Weixin Mini Program |
| 1114 | Android phone sidebar, open Weixin Mini Program |
| 1117 | In the admin page running Weixin Mini Program in the background, open the Mini Program |
# 4. Exit status
Start from base library version 2.7.4. Please remaining backward compatible.
The page callback functiononSaveExitStateis called whenever Weixin Mini Program can be destroyed before Weixin Mini Program can possibly be destroyed.If you want to retain the state in the page, you can "save" some data in this callback function and get the saved data the next time you start byexitState.
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 // 超时时刻
}
}
})
OnSaveExitStateThe return value can contain two items:
| Field Name | type | meaning |
|---|---|---|
| data | Any | Data to be saved (JSON compatible only) |
| expireTimeStamp | Number | A timeout moment, after which the data saved is guaranteed to be discarded, by default (current moment + 1 day) |
A more complete example: {% minicode ('ELP5uTmN7E8l')%}
# Note
- If
expireTimeStamp,The saved data is discarded, and the cold boot does not follow therestartStrategyconfiguration, but directly from the front page cold boot. The expiration TimeStampmay be automatically advanced, such as WeChat when the client needs to clean data.- During the lifetime of Weixin Mini Program,
onSaveExitStatemay be called multiple times, with the result of the last call as the final result. - In some exceptional cases (such as when WeChat client is killed directly by the system), this method will not be invoked, and the next cold startup will not follow the
restartStrategyconfiguration, but instead cold startup directly from the front page.