1e41f4b71Sopenharmony_ci# Creating a PageAbility 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciWhen you create a PageAbility in DevEco Studio, DevEco Studio automatically generates the **onCreate()** and **onDestroy()** callbacks in **app.js** and **app.ets**. You need to implement the other lifecycle callbacks in **app.js** and **app.ets**. The following code snippet shows how to create a PageAbility: 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci```ts 7e41f4b71Sopenharmony_ciimport featureAbility from '@ohos.ability.featureAbility'; 8e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'; 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciconst TAG: string = 'MainAbility'; 11e41f4b71Sopenharmony_ciconst domain: number = 0xFF00; 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciclass MainAbility { 14e41f4b71Sopenharmony_ci onCreate() { 15e41f4b71Sopenharmony_ci // Obtain the context and call related APIs. 16e41f4b71Sopenharmony_ci let context = featureAbility.getContext(); 17e41f4b71Sopenharmony_ci context.getBundleName((data, bundleName) => { 18e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ability bundleName:' ,bundleName); 19e41f4b71Sopenharmony_ci }); 20e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Application onCreate'); 21e41f4b71Sopenharmony_ci } 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci onDestroy() { 24e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Application onDestroy'); 25e41f4b71Sopenharmony_ci } 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci onShow(): void { 28e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Application onShow'); 29e41f4b71Sopenharmony_ci } 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci onHide(): void { 32e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Application onHide'); 33e41f4b71Sopenharmony_ci } 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci onActive(): void { 36e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Application onActive'); 37e41f4b71Sopenharmony_ci } 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci onInactive(): void { 40e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Application onInactive'); 41e41f4b71Sopenharmony_ci } 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci onNewWant() { 44e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Application onNewWant'); 45e41f4b71Sopenharmony_ci } 46e41f4b71Sopenharmony_ci} 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ciexport default new MainAbility(); 49e41f4b71Sopenharmony_ci``` 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ciAfter the PageAbility is created, its abilities-related configuration items are displayed in the **config.json** file. The following is an example **config.json** file of an ability named EntryAbility: 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci```json 55e41f4b71Sopenharmony_ci{ 56e41f4b71Sopenharmony_ci ... 57e41f4b71Sopenharmony_ci "module": { 58e41f4b71Sopenharmony_ci ... 59e41f4b71Sopenharmony_ci "abilities": [ 60e41f4b71Sopenharmony_ci { 61e41f4b71Sopenharmony_ci "skills": [ 62e41f4b71Sopenharmony_ci { 63e41f4b71Sopenharmony_ci "entities": [ 64e41f4b71Sopenharmony_ci "entity.system.home" 65e41f4b71Sopenharmony_ci ], 66e41f4b71Sopenharmony_ci "actions": [ 67e41f4b71Sopenharmony_ci "action.system.home" 68e41f4b71Sopenharmony_ci ] 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci ], 71e41f4b71Sopenharmony_ci "orientation": "unspecified", 72e41f4b71Sopenharmony_ci "formsEnabled": false, 73e41f4b71Sopenharmony_ci "name": ".MainAbility", 74e41f4b71Sopenharmony_ci "srcLanguage": "ets", 75e41f4b71Sopenharmony_ci "srcPath": "MainAbility", 76e41f4b71Sopenharmony_ci "icon": "$media:icon", 77e41f4b71Sopenharmony_ci "description": "$string:MainAbility_desc", 78e41f4b71Sopenharmony_ci "label": "$string:MainAbility_label", 79e41f4b71Sopenharmony_ci "type": "page", 80e41f4b71Sopenharmony_ci "visible": true, 81e41f4b71Sopenharmony_ci "launchType": "singleton" 82e41f4b71Sopenharmony_ci }, 83e41f4b71Sopenharmony_ci ... 84e41f4b71Sopenharmony_ci ] 85e41f4b71Sopenharmony_ci ... 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci} 88e41f4b71Sopenharmony_ci``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ciIn the FA model, you can call **getContext** of **featureAbility** to obtain the application context and then use the capabilities provided by the context. 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci **Table 1** featureAbility APIs 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci| API| Description| 97e41f4b71Sopenharmony_ci| -------- | -------- | 98e41f4b71Sopenharmony_ci| getContext() | Obtains the application context.| 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciThe following code snippet shows how to use **getContext()** to obtain the application context and distributed directory: 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci```ts 104e41f4b71Sopenharmony_ciimport featureAbility from '@ohos.ability.featureAbility'; 105e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs'; 106e41f4b71Sopenharmony_ciimport promptAction from '@ohos.promptAction'; 107e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'; 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ciconst TAG: string = 'PagePageAbilityFirst'; 110e41f4b71Sopenharmony_ciconst domain: number = 0xFF00; 111e41f4b71Sopenharmony_ci``` 112e41f4b71Sopenharmony_ci```ts 113e41f4b71Sopenharmony_ci(async (): Promise<void> => { 114e41f4b71Sopenharmony_ci let dir: string; 115e41f4b71Sopenharmony_ci try { 116e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'Begin to getOrCreateDistributedDir'); 117e41f4b71Sopenharmony_ci dir = await featureAbility.getContext().getOrCreateDistributedDir(); 118e41f4b71Sopenharmony_ci promptAction.showToast({ 119e41f4b71Sopenharmony_ci message: dir 120e41f4b71Sopenharmony_ci }); 121e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'distribute dir is ' + dir); 122e41f4b71Sopenharmony_ci let fd: number; 123e41f4b71Sopenharmony_ci let path = dir + '/a.txt'; 124e41f4b71Sopenharmony_ci fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd; 125e41f4b71Sopenharmony_ci fs.close(fd); 126e41f4b71Sopenharmony_ci } catch (error) { 127e41f4b71Sopenharmony_ci hilog.error(domain, TAG, 'getOrCreateDistributedDir failed with : ' + error); 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci})() 130e41f4b71Sopenharmony_ci``` 131