1e41f4b71Sopenharmony_ci# Widget Lifecycle Management
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciWhen creating an ArkTS widget, you need to implement the [FormExtensionAbility](../reference/apis-form-kit/js-apis-app-form-formExtensionAbility.md) lifecycle APIs.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci1. Import related modules to **EntryFormAbility.ets**.
7e41f4b71Sopenharmony_ci    ```ts
8e41f4b71Sopenharmony_ci    import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit';
9e41f4b71Sopenharmony_ci    import { Configuration, Want } from '@kit.AbilityKit';
10e41f4b71Sopenharmony_ci    import { BusinessError } from '@kit.BasicServicesKit';
11e41f4b71Sopenharmony_ci    import { hilog } from '@kit.PerformanceAnalysisKit';
12e41f4b71Sopenharmony_ci    ```
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci2. In **EntryFormAbility.ets**, implement the [FormExtensionAbility](../reference/apis-form-kit/js-apis-app-form-formExtensionAbility.md) lifecycle APIs, including **onAddForm**, whose **want** parameter can be used to obtain the widget information through [FormParam](../reference/apis-form-kit/js-apis-app-form-formInfo.md#formparam).
15e41f4b71Sopenharmony_ci   
16e41f4b71Sopenharmony_ci      ```ts
17e41f4b71Sopenharmony_ci      const TAG: string = 'EntryFormAbility';
18e41f4b71Sopenharmony_ci      const DOMAIN_NUMBER: number = 0xFF00;
19e41f4b71Sopenharmony_ci    
20e41f4b71Sopenharmony_ci      export default class EntryFormAbility extends FormExtensionAbility {
21e41f4b71Sopenharmony_ci        onAddForm(want: Want): formBindingData.FormBindingData {
22e41f4b71Sopenharmony_ci          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onAddForm');
23e41f4b71Sopenharmony_ci    
24e41f4b71Sopenharmony_ci          // ...
25e41f4b71Sopenharmony_ci          // Called when the widget is created. The widget provider should return the widget data binding class.
26e41f4b71Sopenharmony_ci          let obj: Record<string, string> = {
27e41f4b71Sopenharmony_ci            'title': 'titleOnAddForm',
28e41f4b71Sopenharmony_ci            'detail': 'detailOnAddForm'
29e41f4b71Sopenharmony_ci          };
30e41f4b71Sopenharmony_ci          let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj);
31e41f4b71Sopenharmony_ci          return formData;
32e41f4b71Sopenharmony_ci        }
33e41f4b71Sopenharmony_ci    
34e41f4b71Sopenharmony_ci        onCastToNormalForm(formId: string): void {
35e41f4b71Sopenharmony_ci          // The widget provider should do something to respond to the conversion.
36e41f4b71Sopenharmony_ci          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onCastToNormalForm');
37e41f4b71Sopenharmony_ci        }
38e41f4b71Sopenharmony_ci    
39e41f4b71Sopenharmony_ci        onUpdateForm(formId: string): void {
40e41f4b71Sopenharmony_ci          // Override this method to support scheduled updates, periodic updates, or updates requested by the widget host.
41e41f4b71Sopenharmony_ci          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onUpdateForm');
42e41f4b71Sopenharmony_ci          let obj: Record<string, string> = {
43e41f4b71Sopenharmony_ci            'title': 'titleOnUpdateForm',
44e41f4b71Sopenharmony_ci            'detail': 'detailOnUpdateForm'
45e41f4b71Sopenharmony_ci          };
46e41f4b71Sopenharmony_ci          let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj);
47e41f4b71Sopenharmony_ci          formProvider.updateForm(formId, formData).catch((error: BusinessError) => {
48e41f4b71Sopenharmony_ci            hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] updateForm, error:' + JSON.stringify(error));
49e41f4b71Sopenharmony_ci          });
50e41f4b71Sopenharmony_ci        }
51e41f4b71Sopenharmony_ci    
52e41f4b71Sopenharmony_ci        onChangeFormVisibility(newStatus: Record<string, number>): void {
53e41f4b71Sopenharmony_ci          // 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.
54e41f4b71Sopenharmony_ci          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onChangeFormVisibility');
55e41f4b71Sopenharmony_ci        }
56e41f4b71Sopenharmony_ci    
57e41f4b71Sopenharmony_ci        onFormEvent(formId: string, message: string): void {
58e41f4b71Sopenharmony_ci          // If the widget supports event triggering, override this method and implement the trigger.
59e41f4b71Sopenharmony_ci          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onFormEvent');
60e41f4b71Sopenharmony_ci          // ...
61e41f4b71Sopenharmony_ci        }
62e41f4b71Sopenharmony_ci    
63e41f4b71Sopenharmony_ci        onRemoveForm(formId: string): void {
64e41f4b71Sopenharmony_ci          // Delete widget data.
65e41f4b71Sopenharmony_ci          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onRemoveForm');
66e41f4b71Sopenharmony_ci          // Delete the persistent widget instance data.
67e41f4b71Sopenharmony_ci          // Implement this API based on project requirements.
68e41f4b71Sopenharmony_ci        }
69e41f4b71Sopenharmony_ci    
70e41f4b71Sopenharmony_ci        onConfigurationUpdate(config: Configuration) {
71e41f4b71Sopenharmony_ci          // Called when the configuration of the environment where the formExtensionAbility is running is being updated.
72e41f4b71Sopenharmony_ci          // The formExtensionAbility is cleared after 10 seconds of inactivity.
73e41f4b71Sopenharmony_ci          hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onConfigurationUpdate:' + JSON.stringify(config));
74e41f4b71Sopenharmony_ci        }
75e41f4b71Sopenharmony_ci    
76e41f4b71Sopenharmony_ci        onAcquireFormState(want: Want) {
77e41f4b71Sopenharmony_ci          // Called upon a status query request from the widget. By default, the initial widget state is returned.
78e41f4b71Sopenharmony_ci          return formInfo.FormState.READY;
79e41f4b71Sopenharmony_ci        }
80e41f4b71Sopenharmony_ci      }
81e41f4b71Sopenharmony_ci      ```
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci> **NOTE**
85e41f4b71Sopenharmony_ci>
86e41f4b71Sopenharmony_ci> 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-uiability.md) for processing. After the processing is complete, use [updateForm](../reference/apis-form-kit/js-apis-app-form-formProvider.md#updateform) to notify the widget of the update.
87