1# @ohos.app.ability.ShareExtensionAbility (ExtensionAbility for Sharing) 2 3The **ShareExtensionAbility** module, inherited from [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md), provides a share service template. The ShareExtensionAbility provides a convenient way for people to share current contextual information through applications, social media accounts, and other services. For details about the inheritance relationship of each ability, see [Inheritance Relationship](./js-apis-app-ability-ability.md#ability-inheritance-relationship). 4 5> **NOTE** 6> 7> 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. 8> 9> The APIs of this module can be used only in the stage model. 10 11## When to Use 12 13The following uses text sharing as an example. A user selects a piece of text and starts the application to be shared. The application starts the sharing template based on the sharing information and displays the data based on the sharing template content. 14 15## Modules to Import 16 17```ts 18import { ShareExtensionAbility } from '@kit.AbilityKit'; 19``` 20 21## Properties 22 23**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 24 25| Name| Type| Read-only| Mandatory| Description| 26| -------- | -------- | -------- | -------- | -------- | 27| context | [UIExtensionContext](js-apis-inner-application-uiExtensionContext.md) | No| Yes| Context.| 28 29## ShareExtensionAbility.onCreate 30 31onCreate(): void 32 33Called to initialize the service logic when a ShareExtensionAbility is being created. 34 35**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 36 37**Example** 38 39See [Creating a ShareExtensionAbility](#creating-a-shareextensionability). 40 41## ShareExtensionAbility.onSessionCreate 42 43onSessionCreate(want: Want, session: UIExtensionContentSession): void 44 45Called when a **UIExtensionContentSession** instance is created for this ShareExtensionAbility. 46 47**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 48 49**Parameters** 50 51| Name| Type| Mandatory| Description| 52| -------- | -------- | -------- | -------- | 53| want | [Want](js-apis-app-ability-want.md) | Yes| Want information related to the ShareExtensionAbility, including the ability name and bundle name.| 54| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the ShareExtensionAbility.| 55 56**Example** 57 58See [Creating a ShareExtensionAbility](#creating-a-shareextensionability). 59 60## ShareExtensionAbility.onForeground 61 62onForeground(): void 63 64Called when this ShareExtensionAbility is switched from the background to the foreground. 65 66**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 67 68**Example** 69 70See [Creating a ShareExtensionAbility](#creating-a-shareextensionability). 71 72## ShareExtensionAbility.onBackground 73 74onBackground(): void 75 76Called when this ShareExtensionAbility is switched from the foreground to the background. 77 78**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 79 80**Example** 81 82See [Creating a ShareExtensionAbility](#creating-a-shareextensionability). 83 84## ShareExtensionAbility.onSessionDestroy 85 86onSessionDestroy(session: UIExtensionContentSession): void 87 88Called when a **UIExtensionContentSession** instance is destroyed for this ShareExtensionAbility. 89 90**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 91 92**Parameters** 93 94| Name| Type| Mandatory| Description| 95| -------- | -------- | -------- | -------- | 96| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the ShareExtensionAbility.| 97 98**Example** 99 100See [Creating a ShareExtensionAbility](#creating-a-shareextensionability). 101 102## ShareExtensionAbility.onDestroy 103 104onDestroy(): void | Promise<void> 105 106Called when this ShareExtensionAbility is destroyed to clear resources. 107After 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. 108 109**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 110 111**Returns** 112 113| Type | Description | 114| ------------------------------------- | ------------------------------- | 115| void \| Promise<void> | No return value or a Promise object that returns no result.| 116 117**Example** 118 119See [Creating a ShareExtensionAbility](#creating-a-shareextensionability). 120 121## Creating a ShareExtensionAbility 122 123To manually create a ShareExtensionAbility in the DevEco Studio project, perform the following steps: 124 1251. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **ShareExtAbility**. 126 1272. In the **ShareExtAbility** directory, right-click and choose **New > ArkTS File** to create a file named **ShareExtAbility.ets**. 128 129 ```text 130 ├── ets 131 │ ├── ShareExtAbility 132 │ │ ├── ShareExtAbility.ets 133 └ 134 ``` 135 1363. In the **ShareExtAbility.ets** file, import the ShareExtensionAbility module. Customize a class that inherits from **ShareExtensionAbility** and implement the lifecycle callbacks. 137 138 ```ts 139 import { ShareExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 140 141 const TAG: string = "[ShareExtAbility]"; 142 143 export default class ShareExtAbility extends ShareExtensionAbility { 144 onCreate() { 145 console.info(TAG, `onCreate`); 146 } 147 onSessionCreate(want: Want, session: UIExtensionContentSession) { 148 console.info(TAG, `onSessionCreate, want: ${want.abilityName}`); 149 if (want.parameters) { 150 let obj: Record<string, UIExtensionContentSession | object> = { 151 'session': session, 152 'messages': want.parameters.shareMessages 153 } 154 let storage: LocalStorage = new LocalStorage(obj); 155 session.loadContent('pages/Index', storage); 156 } 157 } 158 onForeground() { 159 console.info(TAG, `ononForeground`); 160 } 161 onBackground() { 162 console.info(TAG, `onBackground`); 163 } 164 onSessionDestroy(session: UIExtensionContentSession) { 165 console.info(TAG, `onSessionDestroy`); 166 } 167 onDestroy() { 168 console.info(TAG, `onDestroy`); 169 } 170 } 171 ``` 172 1734. Register the ShareExtensionAbility in the [**module.json5** file](../../quick-start/module-configuration-file.md) of the module in the project. Set **type** to **share** and **srcEntry** to the code path of the ShareExtensionAbility component. 174 175 ```json 176 { 177 "module": { 178 // ... 179 "extensionAbilities": [ 180 { 181 "name": "ShareExtAbility", 182 "icon": "$media:icon", 183 "description": "share", 184 "type": "share", 185 "exported": true, 186 "srcEntry": "./ets/ShareExtAbility/ShareExtAbility.ets" 187 } 188 ] 189 } 190 } 191 ``` 192