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