Mini Program Operation Mechanism

Download

Mini program does not need installation. When it is used for the first time by the user, AppContainer downloads resources of the Mini Program from the server. The downloaded Mini Program resources are cached in the mobile app client for some time. When the Mini Program with cached resources is opened again, the download process is skipped so the Mini Program is opened faster.

Hot Startup and Cold Startup

  • Cold startup: When the user opens a Mini Program that has not been started or has been destroyed, this is called a cold startup. Here, the Mini Program starts the initialization process. When the process is completed, it triggers the onLaunch callback function.
  • Hot startup: When the user opens a Mini Program that has been opened but is running in background, this is called the hot startup. Here, the Mini Program is not destroyed and restarted. Instead, it is switched from background to foreground. The onLaunch callback function is not triggered.

Foreground/Background Running

  • Foreground running: When the user opens a Mini Program for the first time, the Mini Program runs in the foreground.
  • Background running: When the user clicks the close button in the top-right corner to close the Mini Program or press the Home button to leave mobile App, the Mini Program is not destroyed but switched to background.
  • Switching from background to foreground: When the Mini Program that has not been destroyed by the system is reopened or reactivated, it is switched from background to foreground.

The callback function for foreground/background switching can be registered in app.js. When the Mini Program switches from background to foreground, it triggers onShow; conversely it triggers onHide.

Cache

The local cache system can provide store, get and remove ability to control cache. A Mini Program can store at most 10 MB data. There are two types of API: sync API and async API. The sync API will block current task until the method ends and sync API will not block current task.

OperationSync APIAsync APIDescription
Storemy.setStorageSyncmy.setStorageStore the data according to the key, the original data will be overwrite if the key is the same.
Getmy.getStorageSyncmy.getStorageRead cache data
Removemy.removeStorageSyncmy.removeStorageRemove specific data
Clearmy.clearStorageSyncmy.clearStorageClear all the cache data

Destroy

When the user clicks the close button in the top-right corner to close the Mini Program, the Mini Program is not destroyed but switched to background. Only when the Mini Program stays in background for some time or the system resource consumption is too high, it is really destroyed.