1e41f4b71Sopenharmony_ci# app.js 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Application Lifecycle<sup>4+</sup> 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciYou can implement lifecycle logic specific to your application in the **app.js** file. Available application lifecycle functions are as follows: 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci- **onCreate()**: called when an application is created 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci- **onDestroy()**: called when an application is destroyed 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciIn the following example, logs are printed only in the lifecycle functions. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```js 18e41f4b71Sopenharmony_ci// app.js 19e41f4b71Sopenharmony_ciexport default { 20e41f4b71Sopenharmony_ci onCreate() { 21e41f4b71Sopenharmony_ci console.info('Application onCreate'); 22e41f4b71Sopenharmony_ci }, 23e41f4b71Sopenharmony_ci onDestroy() { 24e41f4b71Sopenharmony_ci console.info('Application onDestroy'); 25e41f4b71Sopenharmony_ci }, 26e41f4b71Sopenharmony_ci} 27e41f4b71Sopenharmony_ci``` 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## Application Object<sup>10+</sup> 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| Attribute | Type | Description | 32e41f4b71Sopenharmony_ci| ------ | -------- | ---------------------------------------- | 33e41f4b71Sopenharmony_ci| getApp | Function | Obtains the data object exposed in **app.js** from the page JS file. This API works globally.| 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci> **NOTE** 36e41f4b71Sopenharmony_ci> 37e41f4b71Sopenharmony_ci> 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. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciThe following is an example: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ciDeclare the application object in **app.js**. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci```javascript 44e41f4b71Sopenharmony_ci// app.js 45e41f4b71Sopenharmony_ciexport default { 46e41f4b71Sopenharmony_ci data: { 47e41f4b71Sopenharmony_ci test: "by getAPP" 48e41f4b71Sopenharmony_ci }, 49e41f4b71Sopenharmony_ci onCreate() { 50e41f4b71Sopenharmony_ci console.info('Application onCreate'); 51e41f4b71Sopenharmony_ci }, 52e41f4b71Sopenharmony_ci onDestroy() { 53e41f4b71Sopenharmony_ci console.info('Application onDestroy'); 54e41f4b71Sopenharmony_ci }, 55e41f4b71Sopenharmony_ci}; 56e41f4b71Sopenharmony_ci``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciAccess the application object on a specific page. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci```javascript 61e41f4b71Sopenharmony_ci// index.js 62e41f4b71Sopenharmony_ciexport default { 63e41f4b71Sopenharmony_ci data: { 64e41f4b71Sopenharmony_ci title: "" 65e41f4b71Sopenharmony_ci }, 66e41f4b71Sopenharmony_ci onInit() { 67e41f4b71Sopenharmony_ci if (typeof getApp !== 'undefined') { 68e41f4b71Sopenharmony_ci var appData = getApp().data; 69e41f4b71Sopenharmony_ci if (typeof appData !== 'undefined') { 70e41f4b71Sopenharmony_ci this.title = appData.name; // read from app data 71e41f4b71Sopenharmony_ci } 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci }, 74e41f4b71Sopenharmony_ci clickHandler() { 75e41f4b71Sopenharmony_ci if (typeof getApp !== 'undefined') { 76e41f4b71Sopenharmony_ci var appData = getApp().data; 77e41f4b71Sopenharmony_ci if (typeof appData !== 'undefined') { 78e41f4b71Sopenharmony_ci appData.name = this.title; // write to app data 79e41f4b71Sopenharmony_ci } 80e41f4b71Sopenharmony_ci } 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci} 83e41f4b71Sopenharmony_ci``` 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci> **NOTE** 86e41f4b71Sopenharmony_ci> 87e41f4b71Sopenharmony_ci> 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