1# Application Model Development 2 3## How do I obtain a notification when the device orientation changes? 4 5Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 6 7**Solution** 8 9Use the **UIAbility.onConfigurationUpdate\(\)** callback to subscribe to system environment variable changes (including the language, color mode, and screen orientation). 10 11**Reference** 12 13[Subscribing to System Environment Variable Changes](../application-models/subscribe-system-environment-variable-changes.md#using-uiability-for-subscription) 14 15## How do I redirect a user to a specified page after they touch a service widget? 16 17Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 18 19**Solution** 20 21Configure a widget event with the redirected-to UIAbility specified, and call **loadContent** in the **onWindowStageCreate\(\)** callback of the target UIAbility to redirect to the specified page. 22 23**Reference** 24 25[Developing Widget Events](../application-models/arkts-ui-widget-configuration.md) 26 27## How do I create a background service in the stage model? 28 29Applicable to: OpenHarmony 3.2 Beta5 30 31**Symptom** 32 33**ServiceExtensionAbility** in the stage model is a system API. Therefore, third-party applications cannot use it to create a background service. 34 35**Solution** 36 37Create a background task to provide the background service. 38 39**Reference** 40 41[Background Task](../task-management/background-task-overview.md) 42 43## Can I create a UIAbility and specify the process to run the UIAbility in the FA and Stage models? 44 45Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 46 47**Solution** 48 49Yes. 50 51- FA model 52 53 The FA model supports multiple processes. By default, all components of an application run in the same process. This default scenario is suitable for most applications. To run a specific component in an independent process, configure the **process** tag under **ability** in the configuration file. Note that this tag is available only for system applications. 54 55- Stage model 56 57 The stage model supports multiple processes. The process model is defined by the system, and third-party applications cannot be configured with multiple processes. To customize an independent process, you must request special permissions, and then specify the **process** tag under **module** in the configuration file. This tag specifies the process in which all the abilities in an HAP run. If this tag is not set, the bundle name is used as the process name by default. 58 59 60## What are the differences between the stage model and the FA model in intra-process object sharing? 61 62Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 63 64**Solution** 65 66- In the stage model, multiple application components share the same ArkTS engine instance. Therefore, they can easily share objects and state with each other. This also reduces the memory usage of complex applications. 67- In the FA model, each application component exclusively uses an ArkTS engine instance. Therefore, you are advised to use the stage model when developing complex applications in distributed scenarios. 68 69**Reference** 70 71[Data Synchronization Between UIAbility and UI](../application-models/uiability-data-sync-with-ui.md) 72 73## How do I use the lifecycle functions of AbilityStage? 74 75Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 76 77**Solution** 78 79Add the field **"srcEntry": "./ets/myabilitystage/MyAbilityStage.ts"** under **module** in the **module.json5** file. 80 81**Reference** 82 83[AbilityStage Component Container](../application-models/abilitystage.md) 84 85 86## How do I delete the mission snapshot in Recents after terminateself is called in the multiton scenario? 87 88Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 89 90**Solution** 91 92You can set **removeMissionAfterTerminate** to **true** in the **module.json5** file. 93 94## Why can't I start a UIAbility instance by using startAbility\(\)? 95 96Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 97 98**Solution** 99 100- If the UIAbility is started using **startAbility**, check whether the **abilityName** field in **want** uses the bundle name as the prefix. If yes, delete the bundle name. 101- Make sure the UIAbility's home page file configured by **onWindowStageCreate** in the **MainAbility.ts** file is defined in the **main\_pages.json** file. You are advised to use the SDK and OpenHarmony SDK versions released on the same day. 102 103## How do I prevent "this" in a method from changing to "undefined" when the method is called? 104 105Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 106 107**Solution** 108 109Method 1: Add **.bind\(this\)** when calling the method. 110 111Method 2: Use the arrow function. 112 113## What should I do when the error message "must have required property 'startWindowIcon'" is displayed during the UIAbility startup? 114 115Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 116 117**Solution** 118 119Configure the **startWindowIcon** attribute under **abilities** in the **module.json5** file. 120 121**Example** 122 123``` 124{ 125 "module": { 126 // Do something. 127 "abilities": [{ 128 // Do something. 129 "startWindowIcon": "$media:space", 130 "startWindowBackground": "$color:white", 131 }] 132 } 133} 134``` 135 136**Reference** 137 138[module.json5 File](../quick-start/module-configuration-file.md) 139 140## Can I obtain the context through globalThis in the stage model? 141 142Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 143 144Do not use **globalThis** to obtain the context in the stage model. 145 146This is because all the processes of an application share a JS VM instance in the stage model. Multiple abilities can run on these processes and share the same global object. If **globalThis** is used, the context of different abilities of the same JS VM instance may be returned. 147 148**Reference** 149 150[Data Synchronization Between UIAbility and UI](../application-models/uiability-data-sync-with-ui.md) 151 152## What should I do when an error indicating too large size is reported during HAP deployment? 153 154Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 155 156**Symptom** 157 158During HAP deployment, the following error message is displayed: 159 160Failure\[INSTALL\_FAILED\_SIZE\_TOO\_LARGE\] error while deploying hap? 161 162**Solution** 163 164You can split the HAP into multiple HAPs. 165 166## How is data returned when startAbilityForResult is called? 167 168Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 169 170**Solution** 171 172The target UIAbilities uses **AbilityContext.terminateSelfWithResult** to terminate itself and pass the result to **startAbilityForResult**. 173 174**Reference** 175 176[Starting UIAbility in the Same Application and Obtaining the Return Result](../application-models/uiability-intra-device-interaction.md) 177 178 179## How do I obtain the system timestamp? 180 181Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 182 183**Solution** 184 185Use **getCurrentTime** of **@ohos.systemDateTime** to obtain the system time and time zone. 186 187**Example** 188 189Use the **@ohos.systemDateTime** API as follows: 190 191 ``` 192 try { 193 systemDateTime.getCurrentTime(true, (error, time) => { 194 if (error) { 195 console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`); 196 return; 197 } 198 console.info(`Succeeded in getting currentTime : ${time}`); 199 }); 200 } catch(e) { 201 console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`); 202 } 203 ``` 204 205 206**Reference** 207 208[System time and time zone] (../reference/apis/js-apis-system-date-time.md#systemdatetimegetcurrenttime) 209 210## How do I obtain the cache directory of the current application? 211 212Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 213 214**Solution** 215 216Use **Context.cacheDir** to obtain the cache directory of the application. 217 218**Reference** 219 220[cacheDir](../application-models/application-context-stage.md#obtaining-application-file-paths) 221 222## In which JS file is the service widget lifecycle callback invoked? 223 224Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 225 226**Solution** 227 228When a widget is created, a **FormAblity.ts** file is generated, which contains the widget lifecycle. 229 230**Reference** 231 232[FormExtensionAbility](../reference/apis/js-apis-app-form-formExtensionAbility.md) 233 234## What should I do when the compilation on DevEco Studio fails while ServiceExtensionAbility and DataShareExtensionAbility APIs are used? 235 236Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 237 238**Symptom** 239 240After the **ServiceExtensionAbility** and **DataShareExtensionAbility** APIs are used, DevEco Studio reports an error indicating that the compilation fails. 241 242**Cause** 243 244The following types of SDKs are provided: 245 246- Public SDK: intended for application developers and does not contain system APIs that require system permissions. 247- Full SDK: intended for original equipment manufacturers (OEMs) and contains system APIs that require system permissions. 248 249The SDK downloaded using DevEco Studio is the public SDK. 250 251**Solution** 252 253Third-party application cannot use **ServiceExtensionAbility** and **DataShareExtensionAbility**. To develop a system application, first [download the full SDK](../faqs/full-sdk-switch-guide.md). 254 255## How do I obtain the temp and files paths at the application level? 256 257Applicable to: OpenHarmony 3.2 Beta5 258 259**Solution** 260 261Obtain them from the application context. Specifically, use **this.context.getApplicationContext.tempDir** i to obtain the **temp** path, and use **this.context.getApplicationContext.filesDir** to obtain the **files** path. 262 263**Reference** 264 265[Obtaining Application File Paths](../application-models/application-context-stage.md#obtaining-application-file-paths) 266 267## Why the application is not deleted from the background mission list after it calls terminateSelf? 268 269Applicable to: OpenHarmony 3.2 Beta5 270 271**Solution** 272 273This is because the **removeMissionAfterTerminate** field under **abilities** in the **module.json5** file of the UIAbility is set to **false** (default value). To enable the application snapshot to be automatically deleted when the application is destroyed, set this field to **true**. 274 275**Reference** 276 277[module.json5 File](../quick-start/module-configuration-file.md) 278 279## How does an application developed in the stage model start an application developed in the FA model? 280 281Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 282 283**Solution** 284 285Refer to the code snippet below: 286 287``` 288let want = { 289 deviceId: "", // An empty deviceId indicates the local device. 290 bundleName: "com.example.myapplication", 291 abilityName: "EntryAbility", 292 moduleName: "Module1", // moduleName is optional. 293 parameters: { // Custom information. 294 }, 295} 296// context is the AbilityContext of the FA model to be started. 297context.startAbility(want).then(() => { 298 ... 299}).catch((err) => { 300 ... 301}) 302``` 303 304## Can atomic services be implemented using JavaScript code only? 305 306Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 307 308**Solution** 309 310Currently, the directory structure of new widgets is css+hml+json. This means that the widgets cannot be implemented by using JavaScript code only. Event triggering and parameter transfer can be processed in JSON files. 311 312## Can the lifecycle callback of a released FA widget be triggered when the widget is displayed in the service center so that the user login information can be obtained without opening the FA application? 313 314Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 315 316**Solution** 317 318After a widget is added, the **onCreate\(\)** lifecycle is triggered so that related user information (silent login) can be displayed even when the application is not started. However, users must manually add the widget after the application is installed. 319 320## What should I do when the error message "\[c4d4d3492eb8531, 0, 0\] ContextDeal::startAbility fetchAbilities failed" is displayed during switching to another application? 321 322Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 323 324**Symptom** 325 326The **startAbility** API reports an error during redirection. 327 328**Solution** 329 330Use **startAbility** for implementation as follows: 331 332``` 333import featureAbility from '@ohos.ability.featureAbility' 334function onStartRemoteAbility() { 335console.info('onStartRemoteAbility begin'); 336let params; 337let wantValue = { 338 bundleName: 'ohos.samples.etsDemo', 339 abilityName: 'ohos.samples.etsDemo.RemoteAbility', 340 deviceId: getRemoteDeviceId(), 341 parameters: params 342}; 343console.info('onStartRemoteAbility want=' + JSON.stringify(wantValue)); 344featureAbility.startAbility({ 345 want: wantValue 346}).then((data) => { 347console.info('onStartRemoteAbility finished, ' + JSON.stringify(data)); 348}); 349console.info('onStartRemoteAbility end'); 350} 351``` 352 353**Reference** 354 355See [Starting a Local PageAbility] (../application-models/start-local-pageability.md). 356 357## How do I implement service login by touching a widget? 358 359Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 360 361**Solution** 362 363To create a service widget in the FA model, perform the following steps: 364 3651. Implement lifecycle callbacks for the widget. 366 3672. Configure the widget configuration file. 368 3693. Persistently store widget data. 370 3714. Update widget data. 372 3735. Develop the widget UI page. 374 3756. Develop a widget event. You can start a UIAbility upon the touch and implement service login in the UIAbility. 376 377**Reference** 378 379[Widget Development in the FA Model](../application-models/widget-development-fa.md) 380 381## How do I redirect to the application details page in Settings? 382 383Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 384 385**Solution** 386 387Refer to the following code: 388 389``` 390this.context.startAbility( 391{ 392 action: "action.settings.app.info", 393 parameters: { "settingsParamBundleName": "your app bundlename" } 394}) 395``` 396 397## How do I get UIAbilityContext within the @Component component in the stage model? 398 399Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 400 401**Solution** 402 403You can use **UIAbility.Context** to obtain the context. 404 405**Example** 406 407``` 408import common from '@ohos.app.ability.common'; 409 410@Entry 411@Component 412struct AbilityContextTest { 413 // abilityContext 414 @State UIAbilityInfo: string = 'Obtaining abilityInfo' 415 UIAbilityContext: common.UIAbilityContext 416 417 aboutToAppear() { 418 // Use getContext to obtain the context and convert it to abilityContext. 419 this.UIAbilityContext = getContext(this) as common.UIAbilityContext 420 } 421 422 build() { 423 Row() { 424 Column({ space: 20 }) { 425 Text(this.UIAbilityInfo) 426 .fontSize(20) 427 .onClick(() => { 428 this.UIAbilityInfo = JSON.stringify(this.UIAbilityContext.abilityInfo) 429 console.log(`ContextDemo abilityInfo = ${this.UIAbilityInfo}`) 430 }) 431 } 432 .width('100%') 433 } 434 .height('100%') 435 } 436} 437``` 438 439## What should I do when starting a continuous task fails? 440 441Applicable to: OpenHarmony 3.2 Release (API version 9) 442 443**Symptom** 444 445A ServiceAbility is started by calling **featureAbility.startAbility\(\)**. When the ServiceAbility attempts to start a continuous task, the error message \{"code":201,"message":"BussinessError 201: Permission denied."\} is reported. 446 447**Solution** 448 449To start a continuous task in the background, you must configure the permission **ohos.permission.KEEP\_BACKGROUND\_RUNNING** in the **module.json5** file and declare the background mode for the ability that needs to use the continuous task. 450 451``` 452"module": { 453 "abilities": [ 454 { 455 "backgroundModes": [ 456 "dataTransfer", 457 "location" 458 ], // Background mode 459 } 460 ], 461 "requestPermissions": [ 462 { 463 "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" // Continuous task permission 464 } 465 ] 466} 467``` 468 469**Reference** 470 471[ServiceAbility Configuration Items - backgroundModes](../application-models/serviceability-configuration.md) 472 473[Continuous Task Permission](../security/permission-list.md#ohospermissionkeep_background_running) 474 475[Continuous Task](../task-management/continuous-task.md) 476 477## How do FA widgets exchange data? 478 479Applicable to: OpenHarmony 3.2 Beta5 (API version 9) 480 481The widget interacts with the widget provider through the **postCardAction** API, and the provider updates data through the **updateForm** API. 482 483**Reference** 484 485[Widget Development in the FA Model](../application-models/widget-development-fa.md) 486