harmony 鸿蒙Widget Lifecycle Management
Widget Lifecycle Management
When creating an ArkTS widget, you need to implement the FormExtensionAbility lifecycle APIs.
Import related modules to EntryFormAbility.ets.
import { formBindingData, FormExtensionAbility, formInfo, formProvider } from '@kit.FormKit'; import { Configuration, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit';
In EntryFormAbility.ets, implement the FormExtensionAbility lifecycle API, including onAddForm, in which want can be used to obtain the widget information through FormParam.
const TAG: string = 'EntryFormAbility'; const DOMAIN_NUMBER: number = 0xFF00; export default class EntryFormAbility extends FormExtensionAbility { onAddForm(want: Want): formBindingData.FormBindingData { hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onAddForm'); hilog.info(DOMAIN_NUMBER, TAG, want.parameters?.[formInfo.FormParam.NAME_KEY] as string); // ... // Called when the widget host creates a widget. The widget provider should return the widget data binding class. let obj: Record<string, string> = { 'title': 'titleOnAddForm', 'detail': 'detailOnAddForm' }; let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj); return formData; } onCastToNormalForm(formId: string): void { // Called when the widget host converts a temporary widget into a normal one. The widget provider should respond to the conversion. // 1. Temporary widgets and normal widgets are defined from the viewpoint of the widget host. // 2. Temporary widgets have a brief existence, appearing following particular events or user interactions and vanishing automatically upon task completion. // 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. // 4. Currently, temporary widgets are not used on mobile phones. hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onCastToNormalForm'); } onUpdateForm(formId: string): void { // Override this method to support scheduled updates, periodic updates, or updates requested by the widget host. hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onUpdateForm'); let obj: Record<string, string> = { 'title': 'titleOnUpdateForm', 'detail': 'detailOnUpdateForm' }; let formData: formBindingData.FormBindingData = formBindingData.createFormBindingData(obj); formProvider.updateForm(formId, formData).catch((error: BusinessError) => { hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] updateForm, error:' + JSON.stringify(error)); }); } onChangeFormVisibility(newStatus: Record<string, number>): void { // 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. hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onChangeFormVisibility'); } onFormEvent(formId: string, message: string): void { // If the widget supports event triggering, override this method and implement the trigger. hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onFormEvent'); // ... } onRemoveForm(formId: string): void { // Delete widget data. hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onRemoveForm'); // Delete the persistent widget instance data. // Implement this API based on project requirements. } onConfigurationUpdate(config: Configuration) { // Called when the configuration of the environment where the formExtensionAbility is running is being updated. // The formExtensionAbility is cleared after 10 seconds of inactivity. hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onConfigurationUpdate:' + JSON.stringify(config)); } onAcquireFormState(want: Want) { // Called 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 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 for processing. After the processing is complete, use updateForm to notify the widget of the update.
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Configuring Widget Configuration Files
harmony 鸿蒙Updating Widget Content by Widget Host (for System Applications Only)
harmony 鸿蒙Creating an ArkTS Widget
harmony 鸿蒙Launching the UIAbility of the Widget Provider in the Background Through the call Event
harmony 鸿蒙Overview of ArkTs Widget Page Editing Interaction
harmony 鸿蒙Editing and Updating the Widget Content
harmony 鸿蒙Updating Widget Content Through the message Event
harmony 鸿蒙Widget Event Capability Overview
harmony 鸿蒙Launching the UIAbility of the Widget Provider Through the router Event
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦