• 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
7export default {
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```
31
32
33After 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 MainAbility:
34
35```json
36{
37  "abilities": [
38    {
39      "skills": [
40        {
41          "entities": [
42            "entity.system.home"
43          ],
44          "actions": [
45            "action.system.home"
46          ]
47        }
48      ],
49      "orientation": "unspecified",
50      "visible": true,
51      "srcPath": "MainAbility",
52      "name": ".MainAbility",
53      "srcLanguage": "ets",
54      "icon": "$media:icon",
55      "description": "$string:MainAbility_desc",
56      "formsEnabled": false,
57      "label": "$string:MainAbility_label",
58      "type": "page",
59      "launchType": "singleton"
60    }
61  ]
62}
63```
64
65
66In the FA model, you can call **getContext** of **featureAbility** to obtain the application context and then use the capabilities provided by the context.
67
68
69**Table 1** featureAbility APIs
70
71| API| Description|
72| -------- | -------- |
73| getContext() | Obtains the application context.|
74
75
76The following code snippet shows how to use **getContext()** to obtain the application context and distributed directory:
77
78```ts
79import featureAbility from '@ohos.ability.featureAbility';
80import fs from '@ohos.file.fs';
81
82(async () => {
83  let dir: string;
84  try {
85    console.info('Begin to getOrCreateDistributedDir');
86    dir = await featureAbility.getContext().getOrCreateDistributedDir();
87    console.info('distribute dir is ' + dir)
88  } catch (error) {
89    console.error('getOrCreateDistributedDir failed with ' + error);
90  }
91
92  let fd: number;
93  let path = dir + "/a.txt";
94  fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd;
95  fs.close(fd);
96})()
97```
98