This specification defines the MiniApp lifecycle events and the process to manage MiniApp and each page's lifecycle. Implementing this specification enables the user agent to manage the lifecycle events of both global application lifecycle and page lifecycle.

本规范定义了MiniApp生命周期的事件与管理MiniApp应用及各个MiniApp页面的生命周期流程。通过实施本规范可以实现user agent对MiniApp全局应用生命周期与MiniApp页面生命周期的管理。

This is still a work in progress. The proposal is being incubated in the W3C MiniApps Ecosystem Community Group.

本文档由 W3C MiniApps生态社区组孵化,目前尚在编辑草稿阶段。

Background 背景

Application lifecycle mechanism provides a means to manage the lifecycle events of both global application lifecycle and page lifecycle. Developing MiniApp with knowledge of the application’s and page’s lifecycle state can lead to improved user experiences. MiniApp lifecycle includes a set of events, with which MiniApp can choose to alter its behavior based on its state.

应用生命周期机制提供了一种管理生命周期事件的机制,可以管理全局应用生命周期与页面生命周期。在开发MiniApp时,了解应用与页面的生命周期状态,可以帮助提供更好的用户体验。MiniApp生命周期包括一系列事件,MiniApp可以利用这些事件来基于生命周期状态变更MiniApp的行为。

MiniApp Global Application Lifecycle

MiniApp Global Application Lifecycle States

This spec defines what the lifecycle of a MiniApp global aplication is and adds definition to enable MiniApp to respond to four important lifecycle events commonly performed by MiniApp:

本规范规定MiniApp全局生命周期的定义,以及MiniApp重要的四个生命周期事件:

For each MiniApp, after initialized, it will be either running in the foreground or in the background.

对每一个MiniApp,在初始化后,它会运行在前台或后台。

When user chooses to close the MiniApp by clicking the close button on MiniApp, or go to the mobile phone’s home screen, the MiniApp will not be destroyed immediately, but switch to be running in the background.

当用户选择点击MiniApp的关闭按键来关闭MiniApp时,或者回到手机的桌面时,MiniApp并不会被销毁,而是转为运行在后台。

When user reopens the same MiniApp, MiniApp will switch from running in the background to the foreground.

当用户再次打开同一个MiniApp时,MiniApp会从后台切换为前台运行。

Only when MiniApp runs in the background for more than a specific time duration (e.g., 5 minutes), or occupies too much system resources in the background, the MiniApp will be destroyed.

只有当MiniApp在后台运行了超过一段时间(比如5分钟),或者占用了太多后台系统资源,则MiniApp会被销毁。

This spec formalizes four lifecycle states to support the above:

本规范定义了四个生命周期状态来支持以上的功能:

MiniApp Global Application Lifecycle API

MiniApp global application lifecycle involves the following API:

MiniApp全局应用生命周期包含如下API:

MiniApp Page Lifecycle

MiniApp Page Lifecycle States

This spec defines what the lifecycle of a MiniApp page is and adds definition to enable MiniApp to respond to five important page lifecycle events commonly performed by MiniApp:

本规范规定MiniApp页面生命周期的定义,以及MiniApp重要的五个页面生命周期事件:

The following figure illustrates the MiniApp page lifecycle.

下图描述了MiniApp页面生命周期过程。

MiniApp page lifecycle
MiniApp page lifecycle illustration

This spec formalizes five page lifecycle states to support the above:

本规范定义了五个页面生命周期状态来支持以上的功能:

MiniApp Page Lifecycle API

MiniApp page lifecycle involves the following API:

MiniApp页面生命周期包含如下API:

Privacy and Security 隐私与安全

onShow and onHide event enables developers to know when a MiniApp is visible. By use of onShow event, developers can choose to process and hide the sensitive data, before MiniApp page switches to be running in foreground; the onUnload event provides a notification that the page is being unloaded.

onShow和onHide事件会通知开发者小程序是否可见,当小程序回到前台时,提供onShow事件,开发者可以对敏感数据进行处理,同时提供onUnload事件将在页面被销毁时提示开发者。

Acknowledgments 致谢

暂无