1e41f4b71Sopenharmony_ci# EmbeddedUIExtensionAbility 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci[EmbeddedUIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md) is an [ExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-extensionAbility.md) component of the EMBEDDED_UI type. It provides the capability of embedded UIs across processes. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe EmbeddedUIExtensionAbility must be used together with the [EmbeddedComponent](../reference/apis-arkui/arkui-ts/ts-container-embedded-component.md). Specifically, with the EmbeddedComponent, you can embed the UI provided by the EmbeddedUIExtensionAbility into a [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) of the same application. The EmbeddedUIExtensionAbility runs in a process independent of the UIAbility for UI layout and rendering. It is usually used in modular development scenarios where process isolation is required. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci> **NOTE** 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> Currently, the EmbeddedUIExtensionAbility and **EmbeddedComponent** are supported only on devices configured with multiple processes. 12e41f4b71Sopenharmony_ci> 13e41f4b71Sopenharmony_ci> The **EmbeddedComponent** can be used only in the UIAbility, and the EmbeddedUIExtensionAbility to start must belong to the same application as the UIAbility.<!--Del--> 14e41f4b71Sopenharmony_ci> The EmbeddedUIExtensionAbility supports the multiton pattern and inherits the process model of the UIExtensionAbility. For details about the multiton pattern and process configuration of the UIExtensionAbility, see [UIExtensionAbility](uiextensionability.md).<!--DelEnd--> 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciThe EmbeddedUIExtensionAbility provides related capabilities through the [UIExtensionContext](../reference/apis-ability-kit/js-apis-inner-application-uiExtensionContext.md) and [UIExtensionContentSession](../reference/apis-ability-kit/js-apis-app-ability-uiExtensionContentSession.md). In this document, the started EmbeddedUIExtensionAbility is called the provider, and the EmbeddedComponent that starts the EmbeddedUIExtensionAbility is called the client. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## Developing the EmbeddedUIExtensionAbility Provider 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci### Lifecycle 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ciThe [EmbeddedUIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md) class provides the lifecycle callbacks [onCreate](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityoncreate), [onSessionCreate](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonsessioncreate), [onSessionDestroy](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonsessiondestroy), [onForeground](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonforeground), [onBackground](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonbackground), and [onDestroy](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityondestroy). You must override them as required. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci- **onCreate**: called to initialize the service logic when an EmbeddedUIExtensionAbility is created. 25e41f4b71Sopenharmony_ci- **onSessionCreate**: called when a **UIExtensionContentSession** instance is created for the EmbeddedUIExtensionAbility. 26e41f4b71Sopenharmony_ci- **onSessionDestroy**: called when a **UIExtensionContentSession** instance is destroyed for the EmbeddedUIExtensionAbility. 27e41f4b71Sopenharmony_ci- **onForeground**: called when the EmbeddedUIExtensionAbility is switched from the background to the foreground. 28e41f4b71Sopenharmony_ci- **onBackground**: called when the EmbeddedUIExtensionAbility is switched from the foreground to the background. 29e41f4b71Sopenharmony_ci- **onDestroy**: called to clear resources when the EmbeddedUIExtensionAbility is destroyed. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### How to Develop 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciTo implement a provider, create an [EmbeddedUIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md) in DevEco Studio as follows: 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci1. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **EmbeddedUIExtAbility**. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci2. Right-click the **EmbeddedUIExtAbility** directory, and choose **New > File** to create a file named **EmbeddedUIExtAbility.ets**. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci3. Open the **EmbeddedUIExtAbility.ets** file and import its dependencies. Customize a class that inherits from **EmbeddedUIExtensionAbility** and implement the lifecycle callbacks [onCreate](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityoncreate), [onSessionCreate](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonsessioncreate), [onSessionDestroy](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonsessiondestroy), [onForeground](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonforeground), [onBackground](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonbackground), and [onDestroy](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityondestroy). 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci ```ts 42e41f4b71Sopenharmony_ci import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci const TAG: string = '[ExampleEmbeddedAbility]' 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility { 47e41f4b71Sopenharmony_ci onCreate() { 48e41f4b71Sopenharmony_ci console.log(TAG, `onCreate`); 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci onForeground() { 52e41f4b71Sopenharmony_ci console.log(TAG, `onForeground`); 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci onBackground() { 56e41f4b71Sopenharmony_ci console.log(TAG, `onBackground`); 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci onDestroy() { 60e41f4b71Sopenharmony_ci console.log(TAG, `onDestroy`); 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession) { 64e41f4b71Sopenharmony_ci console.log(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 65e41f4b71Sopenharmony_ci let param: Record<string, UIExtensionContentSession> = { 66e41f4b71Sopenharmony_ci 'session': session 67e41f4b71Sopenharmony_ci }; 68e41f4b71Sopenharmony_ci let storage: LocalStorage = new LocalStorage(param); 69e41f4b71Sopenharmony_ci session.loadContent('pages/extension', storage); 70e41f4b71Sopenharmony_ci } 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci onSessionDestroy(session: UIExtensionContentSession) { 73e41f4b71Sopenharmony_ci console.log(TAG, `onSessionDestroy`); 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci4. Write the entry page file **pages/Index.ets**, which will be loaded in [onSessionDestroy](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md#embeddeduiextensionabilityonsessiondestroy) of the EmbeddedUIExtensionAbility. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci ```ts 81e41f4b71Sopenharmony_ci import { UIExtensionContentSession } from '@kit.AbilityKit'; 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci let storage = LocalStorage.getShared() 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci @Entry(storage) 86e41f4b71Sopenharmony_ci @Component 87e41f4b71Sopenharmony_ci struct Extension { 88e41f4b71Sopenharmony_ci @State message: string = 'EmbeddedUIExtensionAbility Index'; 89e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci build() { 92e41f4b71Sopenharmony_ci Column() { 93e41f4b71Sopenharmony_ci Text(this.message) 94e41f4b71Sopenharmony_ci .fontSize(20) 95e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 96e41f4b71Sopenharmony_ci Button("terminateSelfWithResult").fontSize(20).onClick(() => { 97e41f4b71Sopenharmony_ci this.session?.terminateSelfWithResult({ 98e41f4b71Sopenharmony_ci resultCode: 1, 99e41f4b71Sopenharmony_ci want: { 100e41f4b71Sopenharmony_ci bundleName: "com.example.embeddeddemo", 101e41f4b71Sopenharmony_ci abilityName: "ExampleEmbeddedAbility", 102e41f4b71Sopenharmony_ci }}); 103e41f4b71Sopenharmony_ci }) 104e41f4b71Sopenharmony_ci }.width('100%').height('100%') 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci ``` 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci5. Register the EmbeddedUIExtensionAbility in the [module.json5 file](../quick-start/module-configuration-file.md) of the module in the project. Set **type** to **embeddedUI** and **srcEntry** to the code path of the EmbeddedUIExtensionAbility. 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci ```json 112e41f4b71Sopenharmony_ci { 113e41f4b71Sopenharmony_ci "module": { 114e41f4b71Sopenharmony_ci "extensionAbilities": [ 115e41f4b71Sopenharmony_ci { 116e41f4b71Sopenharmony_ci "name": "EmbeddedUIExtAbility", 117e41f4b71Sopenharmony_ci "icon": "$media:icon", 118e41f4b71Sopenharmony_ci "description": "EmbeddedUIExtAbility", 119e41f4b71Sopenharmony_ci "type": "embeddedUI", 120e41f4b71Sopenharmony_ci "srcEntry": "./ets/EmbeddedUIExtAbility/EmbeddedUIExtAbility.ets" 121e41f4b71Sopenharmony_ci }, 122e41f4b71Sopenharmony_ci ] 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci } 125e41f4b71Sopenharmony_ci ``` 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci## Developing the EmbeddedUIExtensionAbility Client 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ciYou can load the [EmbeddedUIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md) in the application through the [EmbeddedComponent](../reference/apis-arkui/arkui-ts/ts-container-embedded-component.md) on the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) page. Two fields are added to [want](../reference/apis-ability-kit/js-apis-app-ability-want.md).parameters for the EmbeddedUIExtensionAbility: **ohos.extension.processMode.hostSpecified** and **ohos.extension.processMode.hostInstance**. 132e41f4b71Sopenharmony_ci- **ohos.extension.processMode.hostSpecified** specifies the name of the process in which the EmbeddedUIExtensionAbility that is not started for the first time runs. If this field is not configured, the EmbeddedUIExtensionAbility runs in the same process as the [UIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md). Example configuration: "ohos.extension.processMode.hostSpecified": "com.ohos.inentexecutedemo:share." 133e41f4b71Sopenharmony_ci- **ohos.extension.processMode.hostInstance** specifies whether to start the EmbeddedUIExtensionAbility as an independent process. If **false** is passed in, the EmbeddedUIExtensionAbility is started based on the UIExtensionAbility process model. If **true** is passed in, a process is created, regardless of the process model configured for the UIExtensionAbility. Example configuration: "ohos.extension.processMode.hostInstance": "true". 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciIf both fields are configured, **ohos.extension.processMode.hostSpecified** takes precedence, meaning that the EmbeddedUIExtensionAbility runs in the specified process. 136e41f4b71Sopenharmony_ciFor example, add the following content to the home page file **pages/Index.ets**: 137e41f4b71Sopenharmony_ci```ts 138e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 139e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci@Entry 142e41f4b71Sopenharmony_ci@Component 143e41f4b71Sopenharmony_cistruct Index { 144e41f4b71Sopenharmony_ci @State message: string = 'Message: ' 145e41f4b71Sopenharmony_ci private want: Want = { 146e41f4b71Sopenharmony_ci bundleName: "com.example.embeddeddemo", 147e41f4b71Sopenharmony_ci abilityName: "EmbeddedUIExtAbility", 148e41f4b71Sopenharmony_ci parameters: { 149e41f4b71Sopenharmony_ci 'ohos.extension.processMode.hostInstance': 'true' 150e41f4b71Sopenharmony_ci } 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci build() { 154e41f4b71Sopenharmony_ci Row() { 155e41f4b71Sopenharmony_ci Column() { 156e41f4b71Sopenharmony_ci Text(this.message).fontSize(30) 157e41f4b71Sopenharmony_ci EmbeddedComponent(this.want, EmbeddedType.EMBEDDED_UI_EXTENSION) 158e41f4b71Sopenharmony_ci .width('100%') 159e41f4b71Sopenharmony_ci .height('90%') 160e41f4b71Sopenharmony_ci .onTerminated((info: TerminationInfo) => { 161e41f4b71Sopenharmony_ci this.message = 'Terminarion: code = ' + info.code + ', want = ' + JSON.stringify(info.want); 162e41f4b71Sopenharmony_ci }) 163e41f4b71Sopenharmony_ci .onError((error: BusinessError) => { 164e41f4b71Sopenharmony_ci this.message = 'Error: code = ' + error.code; 165e41f4b71Sopenharmony_ci }) 166e41f4b71Sopenharmony_ci } 167e41f4b71Sopenharmony_ci .width('100%') 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci .height('100%') 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci} 172e41f4b71Sopenharmony_ci``` 173