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