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 implement the lifecycle callbacks (as described in the table below) in **app.js** or **app.ets**.
22
23
24**Table 2** PageAbility lifecycle callbacks
25
26| API| Description|
27| -------- | -------- |
28| onCreate() | Called when the ability is created for the first time. You can initialize the application in this callback.|
29| 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.|
30| onActive() | Called when the ability is switched to the foreground and gains focus.|
31| onInactive() | Called when the ability loses focus. An ability loses focus when it is about to enter the background state.|
32| onShow() | Called when the ability is switched from the background to the foreground. In this case, the ability is visible to users.|
33| onHide() | Called when the ability is switched from the foreground to the background. In this case, the ability is invisible to users.|
34
35
36The following figure shows the relationship between lifecycle callbacks and lifecycle states of the PageAbility.
37
38Figure 2 Relationship between lifecycle callbacks and lifecycle states
39
40![fa-pageAbility-lifecycle](figures/fa-pageAbility-lifecycle.png)
41
42
43> **NOTE**
44>
45> - The PageAbility lifecycle callbacks are synchronous.
46> - The **app.js** file provides only the **onCreate** and **onDestroy** callbacks, and the **app.ets** file provides the full lifecycle callbacks.
47