• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.form.FormExtensionAbility (FormExtensionAbility)
2
3The **FormExtensionAbility** module provides lifecycle callbacks invoked when a widget is created, destroyed, or updated.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> The formExtensionAbility is cleared after 10 seconds of inactivity.
10>
11> The following modules cannot be referenced in the FormExtensionAbility, as doing so may cause the program to exit abnormally:
12> - @ohos.ability.particleAbility (ParticleAbility)
13> - @ohos.multimedia.audio (Audio Management)
14> - @ohos.multimedia.camera (Camera Management)
15> - @ohos.multimedia.media (Media)
16> - @ohos.resourceschedule.backgroundTaskManager (Background Task Management)
17
18
19## Modules to Import
20
21```ts
22import { FormExtensionAbility } from '@kit.FormKit';
23```
24
25## FormExtensionAbility
26
27Widget extension class. It provides APIs to notify the widget provider that a widget is being created or the widget visibility status is being changed.
28
29**Model restriction**: This API can be used only in the stage model.
30
31**System capability**: SystemCapability.Ability.Form
32
33### Attributes
34
35**Model restriction**: This API can be used only in the stage model.
36
37**System capability**: SystemCapability.Ability.Form
38
39| Name   | Type                                                        | Readable| Writable| Description                                                        |
40| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ |
41| context | [FormExtensionContext](js-apis-inner-application-formExtensionContext.md) | Yes  | No  | Context of the FormExtensionAbility. This context is inherited from [ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
42
43### FormExtensionAbility.onAddForm
44
45onAddForm(want: Want): formBindingData.FormBindingData
46
47Called to notify the widget provider that a widget is being created.
48
49**Model restriction**: This API can be used only in the stage model.
50
51**Atomic service API**: This API can be used in atomic services since API version 11.
52
53**System capability**: SystemCapability.Ability.Form
54
55**Parameters**
56
57| Name| Type                                  | Mandatory| Description                                                        |
58| ------ | -------------------------------------- | ---- | ------------------------------------------------------------ |
59| want   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Want information of the widget. You can set the **parameters** field to one or more values enumerated in [widget parameters](./js-apis-app-form-formInfo.md#formparam), such as widget ID, widget name, and widget style. The information must be managed as persistent data to facilitate subsequent widget update and deletion.|
60
61**Return value**
62
63| Type                                                        | Description                                                       |
64| ------------------------------------------------------------ | ----------------------------------------------------------- |
65| [formBindingData.FormBindingData](js-apis-app-form-formBindingData.md#formbindingdata) | A **formBindingData.FormBindingData** object containing the data to be displayed on the widget.|
66
67**Example**
68
69```ts
70import { formBindingData, FormExtensionAbility } from '@kit.FormKit';
71import { Want } from '@kit.AbilityKit';
72
73export default class MyFormExtensionAbility extends FormExtensionAbility {
74  onAddForm(want: Want) {
75    console.log(`FormExtensionAbility onAddForm, want: ${want.abilityName}`);
76    let dataObj1: Record<string, string> = {
77      'temperature': '11c',
78      'time': '11:00'
79    };
80
81    let obj1: formBindingData.FormBindingData = formBindingData.createFormBindingData(dataObj1);
82    return obj1;
83  }
84}
85```
86
87### FormExtensionAbility.onCastToNormalForm
88
89onCastToNormalForm(formId: string): void
90
91Called to notify the widget provider that a temporary widget is being converted to a normal one.
92
93**Model restriction**: This API can be used only in the stage model.
94
95**Atomic service API**: This API can be used in atomic services since API version 11.
96
97**System capability**: SystemCapability.Ability.Form
98
99**Parameters**
100
101| Name| Type  | Mandatory| Description                    |
102| ------ | ------ | ---- | ------------------------ |
103| formId | string | Yes  | ID of the widget that requests to be converted to a normal one.|
104
105**Example**
106
107```ts
108import { FormExtensionAbility } from '@kit.FormKit';
109
110export default class MyFormExtensionAbility extends FormExtensionAbility {
111  onCastToNormalForm(formId: string) {
112    console.log(`FormExtensionAbility onCastToNormalForm, formId: ${formId}`);
113  }
114};
115```
116
117### FormExtensionAbility.onUpdateForm
118
119onUpdateForm(formId: string, wantParams?: Record<string, Object>): void
120
121Called to notify the widget provider that a widget is being updated, with update parameters carried. After obtaining the latest data, your application should call [updateForm](js-apis-app-form-formProvider.md#formproviderupdateform) of **formProvider** to update the widget data.
122
123**Model restriction**: This API can be used only in the stage model.
124
125**Atomic service API**: This API can be used in atomic services since API version 11.
126
127**System capability**: SystemCapability.Ability.Form
128
129**Parameters**
130
131| Name| Type  | Mandatory| Description              |
132| ------ | ------ | ---- | ------------------ |
133| formId | string | Yes  | ID of the widget that requests to be updated.|
134| wantParams<sup>12+</sup> | Record<string, Object> | No  | Parameters used for the update.|
135
136**Example**
137
138```ts
139import { formBindingData, FormExtensionAbility, formProvider } from '@kit.FormKit';
140import { BusinessError } from '@kit.BasicServicesKit';
141
142export default class MyFormExtensionAbility extends FormExtensionAbility {
143  onUpdateForm(formId: string, wantParams?: Record<string, Object>) {
144    console.log(`FormExtensionAbility onUpdateForm, formId: ${formId},
145        wantPara: ${wantParams?.['ohos.extra.param.key.host_bg_inverse_color']}`);
146    let param: Record<string, string> = {
147      'temperature': '22c',
148      'time': '22:00'
149    }
150    let obj2: formBindingData.FormBindingData = formBindingData.createFormBindingData(param);
151    formProvider.updateForm(formId, obj2).then(() => {
152      console.log(`FormExtensionAbility context updateForm`);
153    }).catch((error: BusinessError) => {
154      console.error(`FormExtensionAbility context updateForm failed, data: ${error}`);
155    });
156  }
157};
158```
159
160### FormExtensionAbility.onChangeFormVisibility
161
162onChangeFormVisibility(newStatus: Record\<string, number>): void
163
164Called to notify the widget provider that the widget visibility status is being changed.
165This API is valid only for system applications when **formVisibleNotify** is set to **true**.
166
167**Model restriction**: This API can be used only in the stage model.
168
169**System capability**: SystemCapability.Ability.Form
170
171**Parameters**
172
173| Name | Type  | Mandatory| Description                  |
174| ------- | ------ | ---- | ---------------------- |
175| newStatus  | Record\<string, number> | Yes  | ID and visibility status of the widget to be changed.|
176
177**Example**
178
179```ts
180import { formBindingData, FormExtensionAbility, formProvider } from '@kit.FormKit';
181import { BusinessError } from '@kit.BasicServicesKit';
182
183// According to the ArkTS specification, Object.keys and for..in... cannot be used in .ets files to obtain the key value of an object. Use the user-defined function getObjKeys instead.
184// Extract this function to a .ts file and export it. Import this function to the required .ets file before using it.
185function getObjKeys(obj: Object): string[] {
186  let keys = Object.keys(obj);
187  return keys;
188}
189
190export default class MyFormExtensionAbility extends FormExtensionAbility {
191  onChangeFormVisibility(newStatus: Record<string, number>) {
192    console.log(`FormExtensionAbility onChangeFormVisibility, newStatus: ${newStatus}`);
193    let param: Record<string, string> = {
194      'temperature': '22c',
195      'time': '22:00'
196    }
197    let obj2: formBindingData.FormBindingData = formBindingData.createFormBindingData(param);
198
199    let keys: string[] = getObjKeys(newStatus);
200
201    for (let i: number = 0; i < keys.length; i++) {
202      console.log(`FormExtensionAbility onChangeFormVisibility, key: ${keys[i]}, value= ${newStatus[keys[i]]}`);
203      formProvider.updateForm(keys[i], obj2).then(() => {
204        console.log(`FormExtensionAbility context updateForm`);
205      }).catch((error: BusinessError) => {
206        console.error(`Operation updateForm failed. Cause: ${JSON.stringify(error)}`);
207      });
208    }
209  }
210};
211```
212
213### FormExtensionAbility.onFormEvent
214
215onFormEvent(formId: string, message: string): void
216
217Called to instruct the widget provider to process the widget event.
218
219**Model restriction**: This API can be used only in the stage model.
220
221**Atomic service API**: This API can be used in atomic services since API version 11.
222
223**System capability**: SystemCapability.Ability.Form
224
225**Parameters**
226
227| Name | Type  | Mandatory| Description                  |
228| ------- | ------ | ---- | ---------------------- |
229| formId  | string | Yes  | ID of the widget that requests the event.|
230| message | string | Yes  | Event message.            |
231
232**Example**
233
234```ts
235import { FormExtensionAbility } from '@kit.FormKit';
236
237export default class MyFormExtensionAbility extends FormExtensionAbility {
238  onFormEvent(formId: string, message: string) {
239    console.log(`FormExtensionAbility onFormEvent, formId: ${formId}, message: ${message}`);
240  }
241};
242```
243
244### FormExtensionAbility.onRemoveForm
245
246onRemoveForm(formId: string): void
247
248Called to notify the widget provider that a widget is being destroyed.
249
250**Model restriction**: This API can be used only in the stage model.
251
252**Atomic service API**: This API can be used in atomic services since API version 11.
253
254**System capability**: SystemCapability.Ability.Form
255
256**Parameters**
257
258| Name| Type  | Mandatory| Description              |
259| ------ | ------ | ---- | ------------------ |
260| formId | string | Yes  | ID of the widget to be destroyed.|
261
262**Example**
263
264```ts
265import { FormExtensionAbility } from '@kit.FormKit';
266
267export default class MyFormExtensionAbility extends FormExtensionAbility {
268  onRemoveForm(formId: string) {
269    console.log(`FormExtensionAbility onRemoveForm, formId: ${formId}`);
270  }
271};
272```
273
274### FormExtensionAbility.onConfigurationUpdate
275
276onConfigurationUpdate(newConfig: Configuration): void
277
278Called when system configuration items change. The **onConfigurationUpdate** callback is triggered only when the FormExtensionAbility is alive. <!--Del-->Since API version 20, for system applications, the **onConfigurationUpdate** callback within the FormExtensionAbility will be triggered when the system language changes.<!--DelEnd-->
279
280**Model restriction**: This API can be used only in the stage model.
281
282**Atomic service API**: This API can be used in atomic services since API version 11.
283
284**System capability**: SystemCapability.Ability.Form
285
286**Parameters**
287
288| Name| Type| Mandatory| Description|
289| -------- | -------- | -------- | -------- |
290| newConfig | [Configuration](../apis-ability-kit/js-apis-app-ability-configuration.md) | Yes| New configuration.|
291
292**Example**
293
294```ts
295import { FormExtensionAbility } from '@kit.FormKit';
296import { Configuration } from '@kit.AbilityKit';
297
298export default class MyFormExtensionAbility extends FormExtensionAbility {
299  onConfigurationUpdate(newConfig: Configuration) {
300    // This lifecycle callback is triggered only when the configuration is updated while the FormExtensionAbility is alive.
301    // If no operation is performed within 10 seconds after a FormExtensionAbility instance is created, the instance will be deleted.
302    console.log(`onConfigurationUpdate, config: ${JSON.stringify(newConfig)}`);
303  }
304};
305```
306
307### FormExtensionAbility.onAcquireFormState
308
309onAcquireFormState?(want: Want): formInfo.FormState
310
311Called to notify the widget provider that the widget host is requesting the widget state. By default, the initial widget state is returned. (You can override this API as required.)
312
313**Model restriction**: This API can be used only in the stage model.
314
315**Atomic service API**: This API can be used in atomic services since API version 11.
316
317**System capability**: SystemCapability.Ability.Form
318
319**Parameters**
320
321| Name| Type| Mandatory| Description|
322| -------- | -------- | -------- | -------- |
323| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes| Description of the widget state, including the bundle name, ability name, module name, widget name, and widget dimension.|
324
325**Return value**
326
327| Type                                                        | Description                                                       |
328| ------------------------------------------------------------ | ----------------------------------------------------------- |
329| [formInfo.FormState](js-apis-app-form-formInfo.md#formstate) | Enumerated values of the current widget status.|
330
331**Example**
332
333```ts
334import { FormExtensionAbility, formInfo } from '@kit.FormKit';
335import { Want } from '@kit.AbilityKit';
336
337export default class MyFormExtensionAbility extends FormExtensionAbility {
338  onAcquireFormState(want: Want) {
339    console.log(`FormExtensionAbility onAcquireFormState, want: ${want}`);
340    return formInfo.FormState.UNKNOWN;
341  }
342};
343```
344
345### FormExtensionAbility.onStop<sup>12+</sup>
346
347onStop?(): void
348
349Called when the widget process of the widget provider exits.
350
351**Model restriction**: This API can be used only in the stage model.
352
353**Atomic service API**: This API can be used in atomic services since API version 12.
354
355**System capability**: SystemCapability.Ability.Form
356
357**Example**
358
359```ts
360import { FormExtensionAbility } from '@kit.FormKit';
361
362export default class MyFormExtensionAbility extends FormExtensionAbility {
363  onStop() {
364    console.log(`FormExtensionAbility onStop`);
365  }
366}
367```
368
369### FormExtensionAbility.onFormLocationChanged<sup>20+</sup>
370
371onFormLocationChanged(formId: string, newFormLocation: formInfo.FormLocation): void
372
373Called when the widget location changes.
374
375**Model restriction**: This API can be used only in the stage model.
376
377**System capability**: SystemCapability.Ability.Form
378
379**Parameters**
380
381| Name| Type| Mandatory| Description|
382| -------- | -------- | -------- | -------- |
383| formId | string | Yes| Widget ID.|
384| newFormLocation | [formInfo.FormLocation](js-apis-app-form-formInfo.md#formlocation20) | Yes| Enumerated value of the latest widget location.|
385
386**Example**
387
388```ts
389import { formBindingData, FormExtensionAbility, formInfo } from '@kit.FormKit';
390import { Want } from '@kit.AbilityKit';
391
392export default class EntryFormAbility extends FormExtensionAbility {
393  onAddForm(want: Want) {
394    let formData: Record<string, string | Object> = {
395      'data': 'addForm'
396    };
397    return formBindingData.createFormBindingData(formData);
398  }
399  onFormLocationChanged(formId: string, newFormLocation: formInfo.FormLocation) {
400    console.info("EntryFormAbility onFormLocationChanged current location: " + newFormLocation);
401  }
402}
403```
404
405### FormExtensionAbility.onSizeChanged<sup>20+</sup>
406
407onSizeChanged(formId: string, newDimension: formInfo.FormDimension, newRect: formInfo.Rect): void
408
409Called when the widget size changes.
410
411**Model restriction**: This API can be used only in the stage model.
412
413**Atomic service API**: This API can be used in atomic services since API version 20.
414
415**System capability**: SystemCapability.Ability.Form
416
417**Parameters**
418
419| Name| Type  | Mandatory| Description              |
420| ------ | ------ | ---- | ------------------ |
421| formId | string | Yes  | Widget ID.|
422| newDimension | [formInfo.FormDimension](js-apis-app-form-formInfo.md#formdimension) | Yes| Widget dimension. For example, **Dimension_1_2** indicates a 1 x 2 widget.|
423| newRect | [formInfo.Rect](js-apis-app-form-formInfo.md#rect20) | Yes| Widget position information, including the X and Y coordinates of the widget's top-left corner, as well as its width and height.|
424
425**Example**
426
427```ts
428import { FormExtensionAbility, formInfo } from '@kit.FormKit';
429
430export default class MyFormExtensionAbility extends FormExtensionAbility {
431  onSizeChanged(formId: string, newDimension: formInfo.FormDimension, newRect: formInfo.Rect) {
432    console.info(`FormExtensionAbility onSizeChanged, formId: ${formId}, newDimension: ${newDimension}`);
433  }
434}
435```
436