Widget Lifecycle Management

When creating an ArkTS widget, you need to implement the FormExtensionAbility lifecycle APIs.

  1. Import related modules to EntryFormAbility.ets.

    import formInfo from '@ohos.app.form.formInfo';
    import formBindingData from '@ohos.app.form.formBindingData';
    import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
    import formProvider from '@ohos.app.form.formProvider';
    
  2. In EntryFormAbility.ets, implement the [FormExtensionAbility lifecycle APIs, including onAddForm, whose want parameter can be used to obtain the widget information through FormParam.

    import formInfo from '@ohos.app.form.formInfo';
    import formBindingData from '@ohos.app.form.formBindingData';
    import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
    import formProvider from '@ohos.app.form.formProvider';
    import { Configuration } from '@ohos.app.ability.Configuration';
    import Want from '@ohos.app.ability.Want';
    import Base from '@ohos.base';
    
    export default class EntryFormAbility extends FormExtensionAbility {
     onAddForm(want: Want) {
       console.info('[EntryFormAbility] onAddForm');
       // Called when the widget is created. The widget provider should return the widget data binding class.
       let obj: Record<string, string> = {
         'title': 'titleOnAddForm',
         'detail': 'detailOnAddForm'
       };
       let formData = formBindingData.createFormBindingData(obj);
       return formData;
     }
    
     onCastToNormalForm(formId: string) {
       // The widget provider should do something to respond to the conversion.
       console.info(`[EntryFormAbility] onCastToNormalForm, formId: ${formId}`);
     }
    
     onUpdateForm(formId: string) {
       // Override this method to support scheduled updates, periodic updates, or updates requested by the widget host.
       console.info('[EntryFormAbility] onUpdateForm');
       let obj: Record<string, string> = {
         'title': 'titleOnUpdateForm',
         'detail': 'detailOnUpdateForm'
       };
       let formData = formBindingData.createFormBindingData(obj);
       formProvider.updateForm(formId, formData).catch((err: Base.BusinessError) => {
         console.error(`[EntryFormAbility] Failed to updateForm. Code: ${err.code}, message: ${err.message}`);
       });
     }
    
     onChangeFormVisibility(newStatus: Record<string, number>) {
       // The callback is triggered only when formVisibleNotify is set to true and the application is a system application.
       console.info('[EntryFormAbility] onChangeFormVisibility');
     }
    
     onFormEvent(formId: string, message: string) {
       // If the widget supports event triggering, override this method and implement the trigger.
       console.info('[EntryFormAbility] onFormEvent');
     }
    
     onRemoveForm(formId: string) {
       // The input parameter is the ID of the deleted card.
       console.info('[EntryFormAbility] onRemoveForm');
     }
    
     onConfigurationUpdate(config: Configuration) {
       // Called when the configuration of the environment where the formExtensionAbility is running is being updated.
       // is running is being updated. The formExtensionAbility is cleared after 5 seconds of inactivity.
       console.info('[EntryFormAbility] onConfigurationUpdate:' + JSON.stringify(config));
     }
    
     onAcquireFormState(want: Want) {
       // upon a status query request from the widget. By default, the initial widget state is returned.
       return formInfo.FormState.READY;
     }
    }
    

NOTE

The FormExtensionAbility cannot reside in the background. It persists for 5 seconds after the lifecycle callback is completed and exist 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 5 seconds to complete, it is recommended that you start the application for processing. After the processing is complete, use updateForm() to notify the widget of the update.