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 following modules cannot be referenced in the FormExtensionAbility, as doing so may cause the program to exit abnormally: 10> - @ohos.ability.particleAbility (ParticleAbility) 11> - @ohos.multimedia.audio (Audio Management) 12> - @ohos.multimedia.camera (Camera Management) 13> - @ohos.multimedia.media (Media) 14> - @ohos.resourceschedule.backgroundTaskManager (Background Task Management) 15 16## Modules to Import 17 18```ts 19import { FormExtensionAbility } from '@kit.FormKit'; 20``` 21 22## FormExtensionAbility 23 24Widget extension class. It provides APIs to notify the widget provider that a widget is being created or the widget visibility status is being changed. 25 26**Model restriction**: This API can be used only in the stage model. 27 28**System capability**: SystemCapability.Ability.Form 29 30### Attributes 31 32**Model restriction**: This API can be used only in the stage model. 33 34**System capability**: SystemCapability.Ability.Form 35 36| Name | Type | Readable| Writable| Description | 37| ------- | ------------------------------------------------------------ | ---- | ---- | ------------------------------------------------------------ | 38| 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.| 39 40### FormExtensionAbility.onAddForm 41 42onAddForm(want: Want): formBindingData.FormBindingData 43 44Called to notify the widget provider that a **Form** instance (widget) is being created. 45 46**Model restriction**: This API can be used only in the stage model. 47 48**Atomic service API**: This API can be used in atomic services since API version 11. 49 50**System capability**: SystemCapability.Ability.Form 51 52**Parameters** 53 54| Name| Type | Mandatory| Description | 55| ------ | -------------------------------------- | ---- | ------------------------------------------------------------ | 56| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Want information related to the FormExtensionAbility, including the widget ID, name, and style. The information must be managed as persistent data to facilitate subsequent widget update and deletion.| 57 58**Return value** 59 60| Type | Description | 61| ------------------------------------------------------------ | ----------------------------------------------------------- | 62| [formBindingData.FormBindingData](js-apis-app-form-formBindingData.md#formbindingdata) | A **formBindingData.FormBindingData** object containing the data to be displayed on the widget.| 63 64**Example** 65 66```ts 67import { formBindingData, FormExtensionAbility } from '@kit.FormKit'; 68import { Want } from '@kit.AbilityKit'; 69 70export default class MyFormExtensionAbility extends FormExtensionAbility { 71 onAddForm(want: Want) { 72 console.log(`FormExtensionAbility onAddForm, want: ${want.abilityName}`); 73 let dataObj1: Record<string, string> = { 74 'temperature': '11c', 75 'time': '11:00' 76 }; 77 78 let obj1: formBindingData.FormBindingData = formBindingData.createFormBindingData(dataObj1); 79 return obj1; 80 } 81} 82``` 83 84### FormExtensionAbility.onCastToNormalForm 85 86onCastToNormalForm(formId: string): void 87 88Called to notify the widget provider that a temporary widget is being converted to a normal one. 89 90**Model restriction**: This API can be used only in the stage model. 91 92**Atomic service API**: This API can be used in atomic services since API version 11. 93 94**System capability**: SystemCapability.Ability.Form 95 96**Parameters** 97 98| Name| Type | Mandatory| Description | 99| ------ | ------ | ---- | ------------------------ | 100| formId | string | Yes | ID of the widget that requests to be converted to a normal one.| 101 102**Example** 103 104```ts 105import { FormExtensionAbility } from '@kit.FormKit'; 106 107export default class MyFormExtensionAbility extends FormExtensionAbility { 108 onCastToNormalForm(formId: string) { 109 console.log(`FormExtensionAbility onCastToNormalForm, formId: ${formId}`); 110 } 111}; 112``` 113 114### FormExtensionAbility.onUpdateForm 115 116onUpdateForm(formId: string, wantParams?: Record<string, Object>): void 117 118Called 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#updateform) of **formProvider** to update the widget data. 119 120**Model restriction**: This API can be used only in the stage model. 121 122**Atomic service API**: This API can be used in atomic services since API version 11. 123 124**System capability**: SystemCapability.Ability.Form 125 126**Parameters** 127 128| Name| Type | Mandatory| Description | 129| ------ | ------ | ---- | ------------------ | 130| formId | string | Yes | ID of the widget that requests to be updated.| 131| wantParams<sup>12+</sup> | Record<string, Object> | No | Parameters used for the update.| 132 133**Example** 134 135```ts 136import { formBindingData, FormExtensionAbility, formProvider } from '@kit.FormKit'; 137import { BusinessError } from '@kit.BasicServicesKit'; 138 139export default class MyFormExtensionAbility extends FormExtensionAbility { 140 onUpdateForm(formId: string, wantParams?: Record<string, Object>) { 141 console.log(`FormExtensionAbility onUpdateForm, formId: ${formId}, 142 wantPara: ${wantParams?.['ohos.extra.param.key.host_bg_inverse_color']}`); 143 let param: Record<string, string> = { 144 'temperature': '22c', 145 'time': '22:00' 146 } 147 let obj2: formBindingData.FormBindingData = formBindingData.createFormBindingData(param); 148 formProvider.updateForm(formId, obj2).then(() => { 149 console.log(`FormExtensionAbility context updateForm`); 150 }).catch((error: BusinessError) => { 151 console.error(`FormExtensionAbility context updateForm failed, data: ${error}`); 152 }); 153 } 154}; 155``` 156 157### FormExtensionAbility.onChangeFormVisibility 158 159onChangeFormVisibility(newStatus: Record\<string, number>): void 160 161Called to notify the widget provider that the widget visibility status is being changed. 162This API is valid only for system applications when **formVisibleNotify** is set to **true**. 163 164**Model restriction**: This API can be used only in the stage model. 165 166**System capability**: SystemCapability.Ability.Form 167 168**Parameters** 169 170| Name | Type | Mandatory| Description | 171| ------- | ------ | ---- | ---------------------- | 172| newStatus | Record\<string, number> | Yes | ID and visibility status of the widget to be changed.| 173 174**Example** 175 176```ts 177import { formBindingData, FormExtensionAbility, formProvider } from '@kit.FormKit'; 178import { BusinessError } from '@kit.BasicServicesKit'; 179 180// 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. 181// Extract this function to a .ts file and export it. Import this function to the required .ets file before using it. 182function getObjKeys(obj: Object): string[] { 183 let keys = Object.keys(obj); 184 return keys; 185} 186 187export default class MyFormExtensionAbility extends FormExtensionAbility { 188 onChangeFormVisibility(newStatus: Record<string, number>) { 189 console.log(`FormExtensionAbility onChangeFormVisibility, newStatus: ${newStatus}`); 190 let param: Record<string, string> = { 191 'temperature': '22c', 192 'time': '22:00' 193 } 194 let obj2: formBindingData.FormBindingData = formBindingData.createFormBindingData(param); 195 196 let keys: string[] = getObjKeys(newStatus); 197 198 for (let i: number = 0; i < keys.length; i++) { 199 console.log(`FormExtensionAbility onChangeFormVisibility, key: ${keys[i]}, value= ${newStatus[keys[i]]}`); 200 formProvider.updateForm(keys[i], obj2).then(() => { 201 console.log(`FormExtensionAbility context updateForm`); 202 }).catch((error: BusinessError) => { 203 console.error(`Operation updateForm failed. Cause: ${JSON.stringify(error)}`); 204 }); 205 } 206 } 207}; 208``` 209 210### FormExtensionAbility.onFormEvent 211 212onFormEvent(formId: string, message: string): void 213 214Called to instruct the widget provider to process the widget event. 215 216**Model restriction**: This API can be used only in the stage model. 217 218**Atomic service API**: This API can be used in atomic services since API version 11. 219 220**System capability**: SystemCapability.Ability.Form 221 222**Parameters** 223 224| Name | Type | Mandatory| Description | 225| ------- | ------ | ---- | ---------------------- | 226| formId | string | Yes | ID of the widget that requests the event.| 227| message | string | Yes | Event message. | 228 229**Example** 230 231```ts 232import { FormExtensionAbility } from '@kit.FormKit'; 233 234export default class MyFormExtensionAbility extends FormExtensionAbility { 235 onFormEvent(formId: string, message: string) { 236 console.log(`FormExtensionAbility onFormEvent, formId: ${formId}, message: ${message}`); 237 } 238}; 239``` 240 241### FormExtensionAbility.onRemoveForm 242 243onRemoveForm(formId: string): void 244 245Called to notify the widget provider that a **Form** instance (widget) is being destroyed. 246 247**Model restriction**: This API can be used only in the stage model. 248 249**Atomic service API**: This API can be used in atomic services since API version 11. 250 251**System capability**: SystemCapability.Ability.Form 252 253**Parameters** 254 255| Name| Type | Mandatory| Description | 256| ------ | ------ | ---- | ------------------ | 257| formId | string | Yes | ID of the widget to be destroyed.| 258 259**Example** 260 261```ts 262import { FormExtensionAbility } from '@kit.FormKit'; 263 264export default class MyFormExtensionAbility extends FormExtensionAbility { 265 onRemoveForm(formId: string) { 266 console.log(`FormExtensionAbility onRemoveForm, formId: ${formId}`); 267 } 268}; 269``` 270 271### FormExtensionAbility.onConfigurationUpdate 272 273onConfigurationUpdate(newConfig: Configuration): void 274 275Called when the configuration of the environment where the FormExtensionAbility is running is updated. 276This lifecycle callback is triggered only when the configuration is updated while the FormExtensionAbility is alive. If no operation is performed within 10 seconds after a **FormExtensionAbility** instance is created, the instance will be deleted. 277 278**Model restriction**: This API can be used only in the stage model. 279 280**Atomic service API**: This API can be used in atomic services since API version 11. 281 282**System capability**: SystemCapability.Ability.Form 283 284**Parameters** 285 286| Name| Type| Mandatory| Description| 287| -------- | -------- | -------- | -------- | 288| newConfig | [Configuration](../apis-ability-kit/js-apis-app-ability-configuration.md) | Yes| New configuration.| 289 290**Example** 291 292```ts 293import { FormExtensionAbility } from '@kit.FormKit'; 294import { Configuration } from '@kit.AbilityKit'; 295 296export default class MyFormExtensionAbility extends FormExtensionAbility { 297 onConfigurationUpdate(newConfig: Configuration) { 298 // This lifecycle callback is triggered only when the configuration is updated while the FormExtensionAbility is alive. 299 // If no operation is performed within 10 seconds after a FormExtensionAbility instance is created, the instance will be deleted. 300 console.log(`onConfigurationUpdate, config: ${JSON.stringify(newConfig)}`); 301 } 302}; 303``` 304 305### FormExtensionAbility.onAcquireFormState 306 307onAcquireFormState?(want: Want): formInfo.FormState 308 309Called 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.) 310 311**Model restriction**: This API can be used only in the stage model. 312 313**Atomic service API**: This API can be used in atomic services since API version 11. 314 315**System capability**: SystemCapability.Ability.Form 316 317**Parameters** 318 319| Name| Type| Mandatory| Description| 320| -------- | -------- | -------- | -------- | 321| 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.| 322 323**Example** 324 325```ts 326import { FormExtensionAbility, formInfo } from '@kit.FormKit'; 327import { Want } from '@kit.AbilityKit'; 328 329export default class MyFormExtensionAbility extends FormExtensionAbility { 330 onAcquireFormState(want: Want) { 331 console.log(`FormExtensionAbility onAcquireFormState, want: ${want}`); 332 return formInfo.FormState.UNKNOWN; 333 } 334}; 335``` 336 337### FormExtensionAbility.onStop<sup>12+</sup> 338 339onStop?(): void 340 341Called when the widget process of the widget provider exits. 342 343**Model restriction**: This API can be used only in the stage model. 344 345**Atomic service API**: This API can be used in atomic services since API version 12. 346 347**System capability**: SystemCapability.Ability.Form 348 349**Example** 350 351```ts 352import { FormExtensionAbility } from '@kit.FormKit'; 353 354export default class MyFormExtensionAbility extends FormExtensionAbility { 355 onStop() { 356 console.log(`FormExtensionAbility onStop`); 357 } 358} 359``` 360