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