• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# PageAbility Lifecycle
2
3
4The PageAbility lifecycle defines all states of a PageAbility, such as **INACTIVE**, **ACTIVE**, and **BACKGROUND**. The figure below shows the lifecycle state transition.
5
6**Figure 1** PageAbility lifecycle
7
8![page-ability-lifecycle](figures/page-ability-lifecycle.png)
9
10**Table 1** PageAbility lifecycle states
11
12| State| Description|
13| -------- | -------- |
14| UNINITIALIZED | The PageAbility is not initialized. This is a temporary state, from which a PageAbility changes directly to the **INITIAL** state upon its creation.|
15| INITIAL | The PageAbility is initialized but not running. The PageAbility enters the **INACTIVE** state after it is started.|
16| INACTIVE | The PageAbility is visible but does not gain focus.|
17| ACTIVE | The PageAbility runs in the foreground and has focus.|
18| BACKGROUND | The PageAbility runs in the background. After being re-activated, the PageAbility enters the **ACTIVE** state. After being destroyed, it enters the **INITIAL** state.|
19
20
21You can override the lifecycle callbacks (as described in the table below) in **app.js** or **app.ets**.
22
23**Table 2** PageAbility lifecycle callbacks
24
25| API| Description|
26| -------- | -------- |
27| onCreate() | Called when the ability is created for the first time. You can initialize the application in this callback.|
28| onDestroy() | Called when the ability is destroyed. In this callback, you can make preparations for application exit, such as recycling resources and clearing the cache.|
29| onActive() | Called when the ability is switched to the foreground and gains focus.|
30| onInactive() | Called when the ability loses focus. An ability loses focus when it is about to enter the background state.|
31| onShow() | Called when the ability is switched from the background to the foreground. In this case, the ability is visible to users.|
32| onHide() | Called when the ability is switched from the foreground to the background. In this case, the ability is invisible to users.|
33
34
35The following figure shows the relationship between lifecycle callbacks and lifecycle states of the PageAbility.
36
37Figure 2 Relationship between lifecycle callbacks and lifecycle states
38
39![fa-pageAbility-lifecycle](figures/fa-pageAbility-lifecycle.png)
40
41
42> **NOTE**
43>
44> - The PageAbility lifecycle callbacks are synchronous.
45> - The **app.js** file provides only the **onCreate** and **onDestroy** callbacks, and the **app.ets** file provides the full lifecycle callbacks.
46