Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat program operating mechanism


May 17, 2021 WeChat Mini Program Development Document


Table of contents


The mechanism by which the program runs

Front/background status

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.

The small program starts

In this way, small program startup can be divided into two situations, one is cold start, the other is hot start.

  • Cold Start: If the user opens it for the first time, or if the program is destroyed and then opened by the user again, the small program needs to reload and start, i.e. cold boot.
  • Hot start: If the user has opened a small program, and then open the small program again within a certain period of time, at this time the small program has not been destroyed, but from the background state into the foreline state, this process is hot start.

The timing of the destruction of the small program

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:

  • When a small program enters the background, it can be maintained for a short period of time, and if it does not enter the fore desk during that time, the small program is destroyed.
  • When the small program occupies the system resources, it may be destroyed by the system or take the initiative to be actively recycled by the WeChat client.On iOS, when the WeChat client continuously receives the system memory alarm in a certain time interval, it will take the initiative to destroy the applet, and prompt the user "to run memory, please reopen the applet."The specific policy will continue to adjust optimization.It is recommended that the applet is necessary when necessary wx.onMemoryWarning Listen to the internal departure warning events, make the necessary memory cleanup.
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.

Start scenario classification

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
  • If you enter a scenario with path, you go to the corresponding path page each time you open a small program
  • If you enter a scenario without path: If the small program is hot start, then leave the original state If the small program is cold start, follow the next section of the restart strategy, may be the home page or the last exit page

B. Relaunch to the specified page or home page

Includes scenarios other than Class A

  • If you enter a scene with a path, go to the corresponding path page each time you click
  • If you enter a scene without path, the home page opens each time you enter

The restart policy for the Class A scenario

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.

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:

  • If you exceed expireTimeStamp, the saved data is discarded and does not follow the configuration of restartStrategy at cold startup, but starts directly from the home page.
  • ExpireTimeStamp may be automatically advanced, such as when WeChat clients need to clean up data.
  • During the survival of the small program, onSaveExitState may be called multiple times, with the result of the last call as the final result.
  • In some special cases, such as when a WeChat client is killed directly by the system, this method will not be called, and the next cold boot will not follow the configuration of restartStrategy, but will start directly from the home page cold.