• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Updating Widget Content by Widget Host (for System Applications Only)
2
3
4Widgets that are updated periodically are subject to the scheduled time or interval settings. To offer more flexible updates, the widget host can provide a button to proactively trigger a widget update. Specifically, the widget host calls the [requestForm](../reference/apis/js-apis-app-form-formHost.md#requestform) API to request a widget update. The system then calls the [onUpdateForm](../reference/apis/js-apis-app-form-formExtensionAbility.md#onupdateform) lifecycle callback in the FormExtensionAbility of the widget provider. In the callback, the [updateForm](../reference/apis/js-apis-app-form-formProvider.md#updateform) API can be used to update the widget content. For details about the **onUpdateForm** lifecycle callback, see [Updating Widget Content Through FormExtensionAbility](arkts-ui-widget-event-formextensionability.md).
5
6```ts
7import formHost from '@ohos.app.form.formHost';
8import Base from '@ohos.base';
9
10@Entry()
11@Component
12struct WidgetCard {
13  formId: string = 'formId'; // Widget ID
14
15  build() {
16    Button (`Update Widget`)
17      .type(ButtonType.Capsule)
18      .width('50%')
19      .height(50)
20      .onClick(() => {
21        console.info('FormAbility update form click');
22        // formId is the ID of the widget to be updated.
23        formHost.requestForm(this.formId.toString()).then(() => {
24          console.info('Succeeded in requestForming.');
25        }).catch((error: Base.BusinessError) => {
26          console.error('requestForm fail, error: ' + JSON.stringify(error));
27        })
28      })
29
30    ...
31  }
32}
33```
34