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