1# EmbeddedUIExtensionAbility 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @zhangyafei-echo--> 6<!--Designer: @zhangyafei-echo--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10## Overview 11 12[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. 13 14The 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 separate process, handling the layout and rendering of its pages independently. It does not share data with the UIAbility, making it suitable for modular development scenarios where process isolation is required. 15 16In the example below, the UIAbility runs in the main process and contains multiple EmbeddedComponents. Each EmbeddedComponent corresponds to an EmbeddedUIExtensionAbility. Multiple EmbeddedUIExtensionAbilities can be used to implement different modules in an office suite, such as documents, spreadsheets, and presentations. 17 18**Figure 1** Diagram of EmbeddedUIExtensionAbility 19 20 21 22## Constraints 23 24The EmbeddedUIExtensionAbility and EmbeddedComponent are only supported on devices with multi-process configurations, such as 2-in-1 devices and tablets. 25 26## Lifecycle 27 28The [EmbeddedUIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md) class provides the **onCreate**, **onSessionCreate**, **onSessionDestroy**, **onForeground**, **onBackground**, and **onDestroy** lifecycle callbacks. You must override them as required. The following lifecycle callbacks inherit from [UIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md). 29 30- **onCreate**: called to initialize the service logic when an EmbeddedUIExtensionAbility is created. 31- **onSessionCreate**: called when a UIExtensionContentSession instance is created for the EmbeddedUIExtensionAbility. 32- **onSessionDestroy**: called when a UIExtensionContentSession instance is destroyed for the EmbeddedUIExtensionAbility. 33- **onForeground**: called when the EmbeddedUIExtensionAbility is switched from the background to the foreground. 34- **onBackground**: called when the EmbeddedUIExtensionAbility is switched from the foreground to the background. 35- **onDestroy**: called to clear resources when the EmbeddedUIExtensionAbility is destroyed. 36 37> **NOTE** 38> 39> The **EmbeddedComponent** can be used only in the UIAbility, and the EmbeddedUIExtensionAbility to start must belong to the same application as the UIAbility. 40> 41> <!--Del-->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--> 42 43The 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. 44 45## Developing the EmbeddedUIExtensionAbility Provider 46 47To implement a provider, create an [EmbeddedUIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-embeddedUIExtensionAbility.md) in DevEco Studio as follows: 48 491. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **EmbeddedUIExtAbility**. 50 512. Right-click the **EmbeddedUIExtAbility** directory, and choose **New > File** to create a file named **EmbeddedUIExtAbility.ets**. 52 533. Open the **EmbeddedUIExtAbility.ets** file and import its dependencies. Customize a class that inherits from **EmbeddedUIExtensionAbility** and implement the lifecycle callbacks **onCreate**, **onSessionCreate**, and **onSessionDestroy**, **onForeground**, **onBackground**, and **onDestroy**. 54 55 ```ts 56 import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 57 58 const TAG: string = '[ExampleEmbeddedAbility]'; 59 60 export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility { 61 onCreate() { 62 console.log(TAG, `onCreate`); 63 } 64 65 onForeground() { 66 console.log(TAG, `onForeground`); 67 } 68 69 onBackground() { 70 console.log(TAG, `onBackground`); 71 } 72 73 onDestroy() { 74 console.log(TAG, `onDestroy`); 75 } 76 77 onSessionCreate(want: Want, session: UIExtensionContentSession) { 78 console.log(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 79 let param: Record<string, UIExtensionContentSession> = { 80 'session': session 81 }; 82 let storage: LocalStorage = new LocalStorage(param); 83 session.loadContent('pages/extension', storage); 84 } 85 86 onSessionDestroy(session: UIExtensionContentSession) { 87 console.log(TAG, `onSessionDestroy`); 88 } 89 } 90 ``` 91 924. Write the entry page file **pages/extension.ets**, which will be loaded in **onSessionCreate** of the EmbeddedUIExtensionAbility. 93 94 ```ts 95 import { UIExtensionContentSession } from '@kit.AbilityKit'; 96 97 @Entry() 98 @Component 99 struct Extension { 100 @State message: string = 'EmbeddedUIExtensionAbility Index'; 101 localStorage: LocalStorage | undefined = this.getUIContext().getSharedLocalStorage(); 102 private session: UIExtensionContentSession | undefined = this.localStorage?.get<UIExtensionContentSession>('session'); 103 104 build() { 105 Column() { 106 Text(this.message) 107 .fontSize(20) 108 .fontWeight(FontWeight.Bold) 109 Button('terminateSelfWithResult').fontSize(20).onClick(() => { 110 this.session?.terminateSelfWithResult({ 111 resultCode: 1, 112 want: { 113 bundleName: 'com.example.embeddeddemo', 114 abilityName: 'ExampleEmbeddedAbility' 115 }}); 116 }) 117 }.width('100%').height('100%') 118 } 119 } 120 ``` 121 1225. 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. 123 124 ```json 125 { 126 "module": { 127 "extensionAbilities": [ 128 { 129 "name": "EmbeddedUIExtAbility", 130 "icon": "$media:icon", 131 "description": "EmbeddedUIExtAbility", 132 "type": "embeddedUI", 133 "srcEntry": "./ets/EmbeddedUIExtAbility/EmbeddedUIExtAbility.ets" 134 }, 135 ] 136 } 137 } 138 ``` 139 140 141 142## Developing the EmbeddedUIExtensionAbility Client 143 144You 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**. 145- **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." 146- **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". 147 148If both fields are configured, **ohos.extension.processMode.hostSpecified** takes precedence, meaning that the EmbeddedUIExtensionAbility runs in the specified process. 149For example, add the following content to the home page file **pages/Index.ets**: 150```ts 151import { Want } from '@kit.AbilityKit'; 152import { BusinessError } from '@kit.BasicServicesKit'; 153 154@Entry 155@Component 156struct Index { 157 @State message: string = 'Message: ' 158 private want: Want = { 159 bundleName: 'com.example.embeddeddemo', 160 abilityName: 'EmbeddedUIExtAbility', 161 parameters: { 162 'ohos.extension.processMode.hostInstance': 'true' 163 } 164 } 165 166 build() { 167 Row() { 168 Column() { 169 Text(this.message).fontSize(30) 170 EmbeddedComponent(this.want, EmbeddedType.EMBEDDED_UI_EXTENSION) 171 .width('100%') 172 .height('90%') 173 .onTerminated((info: TerminationInfo) => { 174 this.message = 'Termination: code = ' + info.code + ', want = ' + JSON.stringify(info.want); 175 }) 176 .onError((error: BusinessError) => { 177 this.message = 'Error: code = ' + error.code; 178 }) 179 } 180 .width('100%') 181 } 182 .height('100%') 183 } 184} 185``` 186