1e41f4b71Sopenharmony_ci# @ohos.app.ability.UIExtensionAbility (Base Class for ExtensionAbilities with UI) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci**UIExtensionAbility**, inherited from [ExtensionAbility](js-apis-app-ability-extensionAbility.md), is a base class for ExtensionAbilities with UI in specific scenarios. It provides properties and APIs related to ExtensionAbilities with UI. You cannot inherit from this base class. For details about the inheritance relationship of each ability, see [Inheritance Relationship](./js-apis-app-ability-ability.md#ability-inheritance-relationship). 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs of this module can be used only in the stage model. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Modules to Import 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { UIExtensionAbility } from '@kit.AbilityKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Properties 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| Name| Type| Read-only| Optional| Description| 22e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 23e41f4b71Sopenharmony_ci| context | [UIExtensionContext](js-apis-inner-application-uiExtensionContext.md) | No| No| Context of the UIExtensionAbility.| 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## UIExtensionAbility.onCreate 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_cionCreate(launchParam: AbilityConstant.LaunchParam): void 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciCalled to initialize the service logic when a UIExtensionAbility is being created. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 34e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 35e41f4b71Sopenharmony_ci| launchParam<sup>12+</sup> | [AbilityConstant.LaunchParam](js-apis-app-ability-abilityConstant.md#launchparam) | Yes| Parameters for starting the UIExtensionAbility, and the reason for the last abnormal exit.| 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Example** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```ts 40e41f4b71Sopenharmony_ciimport { UIExtensionAbility, AbilityConstant } from '@kit.AbilityKit'; 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ciconst TAG: string = '[testTag] UIExtAbility'; 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 45e41f4b71Sopenharmony_ci onCreate(launchParam: AbilityConstant.LaunchParam) { 46e41f4b71Sopenharmony_ci console.info(TAG, `onCreate`); 47e41f4b71Sopenharmony_ci console.log(`onCreate, launchParam: ${JSON.stringify(launchParam)}`); 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci} 50e41f4b71Sopenharmony_ci``` 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci## UIExtensionAbility.onSessionCreate 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_cionSessionCreate(want: Want, session: UIExtensionContentSession): void 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciCalled when a **UIExtensionContentSession** instance is created for this UIExtensionAbility. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**Parameters** 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 63e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 64e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | Yes| Want information related to this UIExtensionAbility, including the ability name and bundle name.| 65e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to this UIExtensionAbility.| 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**Example** 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci```ts 70e41f4b71Sopenharmony_ciimport { UIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciconst TAG: string = '[testTag] UIExtAbility'; 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 75e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession) { 76e41f4b71Sopenharmony_ci console.info(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci} 79e41f4b71Sopenharmony_ci``` 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci## UIExtensionAbility.onSessionDestroy 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_cionSessionDestroy(session: UIExtensionContentSession): void 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ciCalled when a **UIExtensionContentSession** instance is destroyed for this UIExtensionAbility. 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**Parameters** 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 92e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 93e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to this UIExtensionAbility.| 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**Example** 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci```ts 98e41f4b71Sopenharmony_ciimport { UIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit'; 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciconst TAG: string = '[testTag] UIExtAbility'; 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 103e41f4b71Sopenharmony_ci onSessionDestroy(session: UIExtensionContentSession) { 104e41f4b71Sopenharmony_ci console.info(TAG, `onSessionDestroy`); 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci} 107e41f4b71Sopenharmony_ci``` 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci## UIExtensionAbility.onForeground 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_cionForeground(): void 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ciCalled when this UIExtensionAbility is switched from the background to the foreground. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci**Example** 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci```ts 120e41f4b71Sopenharmony_ciimport { UIExtensionAbility } from '@kit.AbilityKit'; 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ciconst TAG: string = '[testTag] UIExtAbility'; 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 125e41f4b71Sopenharmony_ci onForeground() { 126e41f4b71Sopenharmony_ci console.info(TAG, `onForeground`); 127e41f4b71Sopenharmony_ci } 128e41f4b71Sopenharmony_ci} 129e41f4b71Sopenharmony_ci``` 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci## UIExtensionAbility.onBackground 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_cionBackground(): void 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciCalled when this UIExtensionAbility is switched from the foreground to the background. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**Example** 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci```ts 142e41f4b71Sopenharmony_ciimport { UIExtensionAbility } from '@kit.AbilityKit'; 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ciconst TAG: string = '[testTag] UIExtAbility'; 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 147e41f4b71Sopenharmony_ci onBackground() { 148e41f4b71Sopenharmony_ci console.info(TAG, `onBackground`); 149e41f4b71Sopenharmony_ci } 150e41f4b71Sopenharmony_ci} 151e41f4b71Sopenharmony_ci``` 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci## UIExtensionAbility.onDestroy 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_cionDestroy(): void | Promise<void> 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**Returns** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci| Type | Description | 160e41f4b71Sopenharmony_ci| ----------------- | ------------------------------------------------------------ | 161e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value. | 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ciCalled to clear resources when this UIExtensionAbility is destroyed. 164e41f4b71Sopenharmony_ciAfter the **onDestroy()** lifecycle callback is executed, the application may exit. Consequently, the asynchronous function (for example, asynchronously writing data to the database) in **onDestroy()** may fail to be executed. You can use the asynchronous lifecycle to ensure that the subsequent lifecycle continues only after the asynchronous function in **onDestroy()** finishes the execution. 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci**Example** 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci```ts 171e41f4b71Sopenharmony_ciimport { UIExtensionAbility } from '@kit.AbilityKit'; 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ciconst TAG: string = '[testTag] UIExtAbility'; 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 176e41f4b71Sopenharmony_ci onDestroy() { 177e41f4b71Sopenharmony_ci console.info(TAG, `onDestroy`); 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci} 180e41f4b71Sopenharmony_ci``` 181