• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Managing ArkTS Widget Lifecycle
2
3
4When creating an ArkTS widget, you need to implement the [FormExtensionAbility](../reference/apis-form-kit/js-apis-app-form-formExtensionAbility.md) lifecycle APIs.
5
61. Import related modules to **EntryFormAbility.ets**.
7    ```ts
8    import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit';
9    import { Configuration, Want } from '@kit.AbilityKit';
10    import { BusinessError } from '@kit.BasicServicesKit';
11    import { hilog } from '@kit.PerformanceAnalysisKit';
12    ```
13
142. In **EntryFormAbility.ets**, implement the [FormExtensionAbility](../reference/apis-form-kit/js-apis-app-form-formExtensionAbility.md) lifecycle API, including **onAddForm**, in which [want](../reference/apis-ability-kit/js-apis-app-ability-want.md) can be used to obtain the widget information through [FormParam](../reference/apis-form-kit/js-apis-app-form-formInfo.md#formparam).
15      ```ts
16      const TAG: string = 'EntryFormAbility';
17      const DOMAIN_NUMBER: number = 0xFF00;
18
19      export default class EntryFormAbility extends FormExtensionAbility {
20        onAddForm(want: Want): formBindingData.FormBindingData {
21          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onAddForm');
22          hilog.info(DOMAIN_NUMBER, TAG, want.parameters?.[formInfo.FormParam.NAME_KEY] as string);
23
24          // ...
25          // Called when the widget host creates a widget. The widget provider should return the widget data binding class.
26          let obj: Record<string, string> = {
27            'title': 'titleOnAddForm',
28            'detail': 'detailOnAddForm'
29          };
30          let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj);
31          return formData;
32        }
33
34        onCastToNormalForm(formId: string): void {
35          // Called when the widget host converts a temporary widget into a normal one. The widget provider should respond to the conversion.
36          // 1. Temporary widgets and normal widgets are defined from the viewpoint of the widget host.
37          // 2. Temporary widgets have a brief existence, appearing following particular events or user interactions and vanishing automatically upon task completion.
38          // 3. Normal widgets maintain a lasting presence, continuing to exist unless explicitly removed or altered by the user. Function widgets developed in normal cases are normal widgets.
39          // 4. Currently, temporary widgets are not used on mobile phones.
40          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onCastToNormalForm');
41        }
42
43        onUpdateForm(formId: string): void {
44          // Override this method to support interval-based updates, time-specific updates, or updates requested by the widget host.
45          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onUpdateForm');
46          let obj: Record<string, string> = {
47            'title': 'titleOnUpdateForm',
48            'detail': 'detailOnUpdateForm'
49          };
50          let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj);
51          formProvider.updateForm(formId, formData).catch((error: BusinessError) => {
52            hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] updateForm, error:' + JSON.stringify(error));
53          });
54        }
55
56        onChangeFormVisibility(newStatus: Record<string, number>): void {
57          // Called when the widget host initiates an event about visibility changes. The widget provider should do something to respond to the notification. This callback takes effect only for system applications.
58          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onChangeFormVisibility');
59        }
60
61        onFormEvent(formId: string, message: string): void {
62          // If the widget supports event triggering, override this method and implement the trigger.
63          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onFormEvent');
64          // ...
65        }
66
67        onRemoveForm(formId: string): void {
68          // Remove widget data.
69          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onRemoveForm');
70          // Remove the persistent widget instance data.
71          // Implement this API based on project requirements.
72        }
73
74        onConfigurationUpdate(config: Configuration) {
75          // Called when the configuration of the environment where the formExtensionAbility is running is being updated.
76          // The formExtensionAbility is cleared after 10 seconds of inactivity.
77          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onConfigurationUpdate:' + JSON.stringify(config));
78        }
79
80        onAcquireFormState(want: Want) {
81          // Called upon a status query request from the widget. By default, the initial widget state is returned.
82          return formInfo.FormState.READY;
83        }
84      }
85      ```
86
87> **NOTE**
88>
89> The FormExtensionAbility cannot reside in the background. It persists for 10 seconds after the lifecycle callback is completed and exits if no new lifecycle callback is invoked during this time frame. This means that continuous tasks cannot be processed in the widget lifecycle callbacks. For the service logic that may take more than 10 seconds to complete, it is recommended that you [start the application](arkts-ui-widget-event-overview.md) for processing. After the processing is complete, use [updateForm](../reference/apis-form-kit/js-apis-app-form-formProvider.md#formproviderupdateform) to notify the widget of the update.
90