# Mini Program operation mechanism

# 1. The Life Cycle of Small Programs

Small programs will experience many different states from startup to final destruction, and Mini programs will behave differently in different states.

小程序生命周期

# 1.1 Mini Program start

From the perspective of user cognition, generalized Mini Program startup can be divided into two situations, one isCold start♪ one isHot start

  • Cold start: If the user opens the Mini Program for the first time, or the Mini Program is opened again by the user after being destroyed, the Mini Program needs to be reloaded to start, that is, cold start.
  • Hot start: If the user has opened a Mini Program, and then open the Mini Program again within a certain period of time, at this time the Mini Program has not been destroyed, but from the background state into the foreground state, this process is a hot start.

From the perspective of the Mini Program life cycle, we generally talk aboutstart-upSpecifically refers to the cold start, hot start is generally called the background cut front.

# 1.2 Front and Back Office

After the Mini Program is started, the interface is shown to the user. At this point, the Mini Programs are inReceptionStatus.

When the userStopMini Program, the Mini Program is not really closed, but into theBackstageState, at which point the Mini Program can also run for a short period of time, but part API Their use will be limited. Ways to cut the background include but are not limited to the following:

  • Click the capsule button in the upper right corner to leave the Mini Program
  • iOS Swipe right from the left side of the screen to leave the Mini Program
  • Android Click the return key to leave the Mini Program
  • When the Mini Program front desk is running, directly cut the WeChat background (gesture or Home Key)
  • Mini Program foreground running direct lock screen

When the user enters WeChat again and opens the mini program, the mini program will re-enter theReceptionStatus.

# 1.3 Hang

Small entry.BackstageState after a period of time (currently it is 5 Second), WeChat will stop the Mini Program JS Execution of the thread, the Mini Program enters theHangStatus. At this point, the Mini Program's memory state is preserved, but developer code execution stops, and event and interface callbacks are returned before the Mini Program enters again.ReceptionWhen triggered.

When the developer uses theBackstage music playbackBackstage Geographic LocationWhen such capabilities are available, the Mini Program can beBackstageContinuous operation, will not enter into theHangstate

# 1.4 Small Program Destruction

If the user has not used the Mini Program for a long time, or the system resources are tight, the Mini Program will beDestroy, that is, complete termination of operation. Specifically, it includes the following situations:

  • When the Mini Program enters the background and isHangLater, if a long time (currently is 30 Minutes) are not re-entered the foreground, the Mini Program will be destroyed.
  • When the mini program takes up too much system resources, it may be destroyed by the system or actively recycled by the WeChat client.
    • in iOS On the other hand, when the WeChat client receives the system memory alarm continuously within a certain time interval, it will take the initiative to destroy the Mini Program according to a certain strategy and prompt the user. Running out of memory, please reopen the Mini ProgramThe specific strategy will be continuously adjusted and optimized.
    • It is recommended that Mini programs be used when necessary wx.onMemoryWarning Listens for memory warning events and performs necessary memory cleanup.

Base library 1.1.0 And above, 1.4.0 The following versions: When the user enters from a scan, forward, etc. (Scene value1007, 1008, 1011, 1025) Enter the Mini Program, and exit without the top Mini Program, the Mini Program will be destroyed.

# 2. Mini Program cold start page

Mini Program cold start, the open page has the following conditions

  • (A Class Scene) If the start scene does not have path
    • Base library 2.8.0 The following version, go to the front page
    • Base library 2.8.0 And the above version followsRestart policy, may be the home page or the last exit page
  • (B Class Scene) If you start a scene with a Path, it starts to enter the corresponding path The page of

# 2.1 Restart policy

Start from base library version 2.8.0. Please remaining backward compatible.

When the Mini Program is cold started, if it is started without path(A Class scene), will go to the front page of the Mini Program by default. On the page corresponding to the json File (which can also be configured globally in the app.json of window Paragraph), specifying the restartStrategy Configuration item can change this default behavior so that after exiting from a page, the next A The cold start of the class scene can be returned to this page.

Code example:

{
  "restartStrategy": "homePage"
}

restartStrategy Optional values:

Optional value meaning
HomePage (Default) If you exit the Mini Program from this page, it will be cold started from the home page next time.
homePageAndLatestPage If you exit the Mini Program from this page, the page loads immediately after the next cold start, the parameters of the page remain unchanged tab Page)

Note: Even if not configured to HomePage , if the Mini Program exits too long (the current default time of one day), you can useExit statusTo adjust), the next cold start will also no longer follow the restartStrategy Of the configuration, but directly from the home cold boot.

In any case, the state in the page is not preserved, such as the text content in the input box, checkbox The check state, etc. will not be restored. If you want to restore or partially restore, you need to utilize theExit status

# 3. Mini Program hot start page

When the Mini Program hot starts, the open page has the following conditions

  • (A Class Scene) If the start scene does not have Path, it retains the state of the last browsing
  • (B Class Scene) If you start a scene with a Path, then reLaunch To the corresponding path The page of

A Common scenarios include the followingScene value

Scene Value ID Introductions
1001 Discovery bar Mini Program main entrance,Recently UsedList (included since version 2.2.4 of the base libraryMy little program.List)
1003 List of Star Label Mini Program
1023 System desktop small icon to open the Mini Program
1038 Return from Other Mini Program
1056 Chat top music player upper right menu, open the Mini Program
1080 Customer service session menu Mini Program entry, open Mini Program
1083 Official Account message template entry , open the Mini Program (only Tencent customer service Mini Program has)
1089 Chat main interface drop down, open the Mini Program/WeChat chat main interface drop down,Recently UsedColumn (base library version 2.2.4 containsMy little program.Column)
1090 Long press the menu in the upper right corner of the Mini Program to open the Mini Programs
1103 Discover - Mini Program main entrance to my Mini Program, open the Mini Program
1104 Chat main interface drop down, from my Mini Program, open the Mini Program
1113 Android phone negative screen, open the Mini Program
1114 Android phone sidebar, open the Mini Program
1117 In the management page of the background running Mini Program, open the Mini Program

# 4. Exit status

Start from base library version 2.7.4. Please remaining backward compatible.

Whenever an Mini Program may be destroyed before the page callback function onSaveExitState Will be called. If you want to preserve the state in the page, you can "save" some data in this callback function, which you can use on the next startup through exitState Access to this saved data.

Code example:

{
  "restartStrategy": "homePageAndLatestPage"
}
Page({
  onLoad: function() {
    var prevExitState = this.exitState // Try to get the last exit before onSaveExitState Saved data
    if (prevExitState !== undefined) { // If it's based on restartStrategy Configure the cold start, you can get the
      prevExitState.myDataField === 'myData' 
    }
  },
  onSaveExitState: function() {
    var exitState = { myDataField: 'myData' } // Data to be saved
    return {
      data: exitState,
      expireTimeStamp: Date.now() + 24 * 60 * 60 * 1000 // Timeout period
    }
  }
})

onSaveExitState The return value can contain two items:

Field name type meaning
Data Any The data that needs to be saved (can only be JSON Compatible data)
expireTimeStamp Number Timeout, after which saved data is guaranteed to be discarded, defaults to (The Current Moment + 1 day)

A more complete example: {% Minicode ('ELP5uTmN7E8l') %}

# Note

  • If more than expireTimeStamp , saved data will be discarded, and the cold boot does not follow the restartStrategy Of the configuration, but directly from the home cold boot.
  • expireTimeStamp It is possible to be automatically advanced, such as when the WeChat client needs to clean data.
  • While the Mini Program survives, onSaveExitState It may be called several times, in which case the result of the last call is the final result.
  • In some special cases (such as WeChat client directly killed by the system), this method will not be called, and the next cold start will not follow. restartStrategy Of the configuration, but directly from the home cold boot.