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