Applet quick start to the project development 3 (applet lifecycle)

First, the application life cycle

App () function is used to register a small program. Accepts a object parameter, a function of its life cycle and other specified applet.

object parameters:

Attributes

Types of

description

Trigger When

onLaunch

Function

Lifecycle function - monitor applet initialization

When the applet initialization is complete, it will trigger onLaunch (Global triggers only once)

onShow

Function

Lifecycle function - monitor applet displays

When the applet is started, or from the background into the foreground display will trigger onShow

onHide

Function

Lifecycle function - small programs hidden listening

When the applet from the foreground into the background, it will trigger onHide

Foreground, background is defined : When the user clicks to close the upper left corner, or press the Home key to leave the micro-channel device, the applet is not directly destroyed, but into the background; micro letter when entering or re-open the applet again, will enter the foreground from background .

Application life cycle

  • The user first opens a small program, triggering onLaunch (Global triggers only once).
  • After the applet initialization is complete, the trigger onShow method, monitor applet displays.
  • Applet from the foreground into the background, triggering onHide method.
  • Applet from the background into the foreground display, triggering onShow method.
  • Applet running in the background a certain time, or system resources too, will be destroyed.

Sample code:

App({

  onLaunch: function() {

// first opened

},

  onShow: function() {

      // applet after initialization is complete

  },

  onHide: function() {

      // applet from the foreground into the background

  },

  onError: function(msg) {

    console.log(msg)

  },

  globalData: 'global data'

})

Applet provides a global getApp () function can be obtained applet instance.

// other.jsvar appInstance = getApp()console.log(appInstance.globalData) // I am global data

Note:
App () must be registered in app.js in, and can not register more than one.

Do not defined in the App (function) in the call getApp (), you can use this app to get the instance.

Do not call getCurrentPage when onLaunch of (), then page has not been generated.

By getApp (after acquiring instance, Do not attempt to call the life-cycle function).

Page life cycle

Page () function is used to register a page. Accepts a object parameter, which specifies the initial data page, lifecycle functions, such as event handler.
object parameters:

Attributes

Types of

description

data

Object

The initial page of data

onLoad

Function

Lifecycle function - listen for page loads

onReady

Function

Lifecycle function - listen first page rendering is complete

onShow

Function

Lifecycle function - page display monitor

onHide

Function

Lifecycle function - monitor page Hide

onUnload

Function

Lifecycle function - monitor page unload

 

 

Page life cycle

  • After the applet registration is complete, the page is loaded, triggering onLoad method.
  • After the page is loaded trigger onShow method to display the page.
  • First display page will trigger onReady method, rendering page elements and styles, a page will only be called once.
  • When the applet running in the background or jump to other pages, triggering onHide method.
  • When the applet has the background to the foreground enter or re-enter the page, triggering onShow method.
  • When using the redirection method wx.redirectTo (OBJECT) or close the current page Back wx.navigateBack (), triggered onUnload.

to sum up:

  • onLoad: page loads.
    1) a page only called once.
    2) parameters, and may acquire wx.navigateTo and wx.redirectTo <navigator /> in the query.
  • onShow: page display
    1) will be called once each time you open the page.
  • onReady: 页面初次渲染完成
    1)一个页面只会调用一次,代表页面已经准备妥当,可以和视图层进行交互。
    2)对界面的设置如wx.setNavigationBarTitle请在onReady之后设置。详见生命周期   
    https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F
  • onHide: 页面隐藏
    1)当navigateTo或底部tab切换时调用。
  • onUnload: 页面卸载
    1)当redirectTo或navigateBack的时候调用。

示例代码:

//index.js

Page({

  data: {

    text: "This is page data."

  },

  onLoad: function(options) {

    // some initialize when page load.

  },

  onReady: function() {

    // something when page ready.

  },

  onShow: function() {

    // something when page show.

  },

  onHide: function() {

    // something when page hide.

  },

  onUnload: function() {

    // something when page close.

  },

  onPullDownRefresh: function() {

    // something when pull down.

  },

  onReachBottom: function() {

    // something when page reach bottom.

  },

  onShareAppMessage: function () {

   // share data when user share.

  },

  // Event handler.

  viewTap: function() {

    this.setData({

      text: 'updating view.'

    })

  },

  customData: {

    Name: 'name'

  }

})

三、用Page 实例说明的页面的生命周期

页面的生命周期

小程序由两大线程组成:负责界面的视图线程(view thread)和负责数据、服务处理的服务线程(appservice thread),两者协同工作,完成小程序页面生命周期的调用。

视图线程有四大状态:

  1. 初始化状态:初始化视图线程所需要的工作,初始化完成后向 “服务线程”发送初始化完成信号,然后进入等待状态,等待服务线程提供初始化数据
  2. 首次渲染状态:当收到服务线程提供的初始化数据后(json和js中的data数据),渲染小程序界面,渲染完毕后,发送“首次渲染完成信号”给服务线程,并将页面展示给用户。
  3. 持续渲染状态:此时界面线程继续一直等待“服务线程”通过this.setdata()函数发送来的界面数据,只要收到就重新局部渲染,也因此只要更新数据并发送信号,界面就自动更新。
  4. 结束状态:页面被回收或者销毁、应用被系统回收、销毁时触发。

服务线程五大状态:

  1. 初始化状态:此阶段仅启动服务线程所需的基本功能,比如信号发送模块。系统的初始化工作完毕,就调用自定义的onload和onshow,然后等待视图线程的“视图线程初始化完成”号。onload是只会首次渲染的时候执行一次,onshow是每次界面切换都会执行,简单理解,这就是唯一差别。
  2. 等待激活状态:接收到“视图线程初始化完成”信号后,将初始化数据发送给“视图线程”,等待视图线程完成初次渲染。
  3. 激活状态:收到视图线程发送来的“首次渲染完成”信号后,就进入激活状态既程序的正常运行状态,并调用自定义的onReady()函数。此状态下就可以通过 this.setData 函数发送界面数据给界面线程进行局部渲染,更新页面。
  4. 后台运行状态:如果界面进入后台,服务线程就进入后台运行状态,从目前的官方解读来说,这个状态挺奇怪的,和激活状态是相同的,也可以通过setdata函数更新界面的。毕竟小程序的框架刚推出,应该后续会有很大不同吧。
  5. 结束状态:页面被回收或者销毁、应用被系统回收、销毁时触发。

四、应用的生命周期对页面生命周期的影响

应用生命周期与页面生命周期之间的关系

  • 小程序初始化完成后,页面首次加载触发onLoad,只会触发一次。
  • 当小程序进入到后台,先执行页面onHide方法再执行应用onHide方法。
  • 当小程序从后台进入到前台,先执行应用onShow方法再执行页面onShow方法。

 

Guess you like

Origin blog.csdn.net/jianpengxuexikaifa/article/details/90743269