• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Creating a PageAbility
2
3
4When you create a PageAbility on 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
7class EntryAbility {
8  onCreate() {
9    console.info('Application onCreate')
10  }
11  onDestroy() {
12    console.info('Application onDestroy')
13  }
14  onShow() {
15    console.info('Application onShow')
16  }
17  onHide() {
18    console.info('Application onHide')
19  }
20  onActive() {
21    console.info('Application onActive')
22  }
23  onInactive() {
24    console.info('Application onInactive')
25  }
26  onNewWant() {
27    console.info('Application onNewWant')
28  }
29}
30
31export default new EntryAbility()
32```
33
34
35After 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:
36
37```json
38{
39  "abilities": [
40    {
41      "skills": [
42        {
43          "entities": [
44            "entity.system.home"
45          ],
46          "actions": [
47            "action.system.home"
48          ]
49        }
50      ],
51      "orientation": "unspecified",
52      "visible": true,
53      "srcPath": "EntryAbility",
54      "name": ".EntryAbility",
55      "srcLanguage": "ets",
56      "icon": "$media:icon",
57      "description": "$string:EntryAbility_desc",
58      "formsEnabled": false,
59      "label": "$string:EntryAbility_label",
60      "type": "page",
61      "launchType": "singleton"
62    }
63  ]
64}
65```
66
67
68In the FA model, you can call **getContext** of **featureAbility** to obtain the application context and then use the capabilities provided by the context.
69
70
71  **Table 1** featureAbility APIs
72
73| API| Description|
74| -------- | -------- |
75| getContext() | Obtains the application context.|
76
77
78The following code snippet shows how to use **getContext()** to obtain the application context and distributed directory:
79
80```ts
81import featureAbility from '@ohos.ability.featureAbility';
82import fs from '@ohos.file.fs';
83
84(async () => {
85  let dir: string;
86  try {
87    console.info('Begin to getOrCreateDistributedDir');
88    dir = await featureAbility.getContext().getOrCreateDistributedDir();
89    console.info('distribute dir is ' + dir);
90    let fd: number;
91    let path = dir + "/a.txt";
92    fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd;
93    fs.close(fd);
94  } catch (error) {
95    console.error('getOrCreateDistributedDir failed with ' + error);
96  }
97})()
98```
99