1# app.js
2
3## Application Lifecycle<sup>4+</sup>
4
5You can implement lifecycle logic specific to your application in the **app.js** file. Available application lifecycle functions are as follows:
6
7
8- **onCreate()**: called when an application is created
9
10- **onDestroy()**: called when an application is destroyed
11
12
13In the following example, logs are printed only in the lifecycle functions.
14
15
16
17```js
18// app.js
19export default {
20  onCreate() {
21    console.info('Application onCreate');
22  },
23  onDestroy() {
24    console.info('Application onDestroy');
25  },
26}
27```
28
29## Application Object<sup>10+</sup>
30
31| Attribute    | Type      | Description                                      |
32| ------ | -------- | ---------------------------------------- |
33| getApp | Function | Obtains the data object exposed in **app.js** from the page JS file. This API works globally.|
34
35> **NOTE**
36>
37> The application object is global data and occupies JS memory before the application exits. Although it facilitates data sharing between different pages, exercise caution when using it on small-system devices, whose memory is small. If they are overused, exceptions may occur due to insufficient memory when the application displays complex pages.
38
39The following is an example:
40
41Declare the application object in **app.js**.
42
43```javascript
44// app.js
45export default {
46    data: {
47        test: "by getAPP"
48    },
49    onCreate() {
50        console.info('Application onCreate');
51    },
52    onDestroy() {
53        console.info('Application onDestroy');
54    },
55};
56```
57
58Access the application object on a specific page.
59
60```javascript
61// index.js
62export default {
63    data: {
64        title: ""
65    },
66    onInit() {
67        if (typeof getApp !== 'undefined') {
68            var appData = getApp().data;
69            if (typeof appData !== 'undefined') {
70                this.title = appData.name; // read from app data
71            }
72        }
73    },
74    clickHandler() {
75        if (typeof getApp !== 'undefined') {
76            var appData = getApp().data;
77            if (typeof appData !== 'undefined') {
78                appData.name = this.title; // write to app data
79            }
80        }
81    }
82}
83```
84
85> **NOTE**
86>
87> To ensure that the application can run properly on an earlier version that does not support **getApp**, compatibility processing must be performed in the code. That is, before using **getApp**, check whether it is available.
88