• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# API Switching Overview
2
3
4Due to the differences in the thread model and process model, certain APIs can be used only in the FA model. They are marked with **FAModelOnly** in the SDK. When switching an application from the FA model to the stage model, replace the APIs marked with **FAModelOnly** in the application with the APIs supported in the stage model. This topic uses the switching of **startAbility()** as an example.
5
6![api-switch-overview](figures/api-switch-overview.png)
7
8
9
10- Sample code of **startAbility()** in the FA model:
11
12  ```ts
13  import featureAbility from '@ohos.ability.featureAbility';
14  import Want from '@ohos.app.ability.Want';
15  import hilog from '@ohos.hilog';
16
17  const TAG: string = 'PagePageAbilityFirst';
18  const domain: number = 0xFF00;
19
20  @Entry
21  @Component
22  struct PagePageAbilityFirst {
23
24    build() {
25      Column() {
26        List({ initialIndex: 0 }) {
27          ListItem() {
28            Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
29              //...
30            }
31            .onClick(() => {
32              (async (): Promise<void> => {
33                try {
34                  hilog.info(domain, TAG, 'Begin to start ability');
35                  let want: Want = {
36                    bundleName: 'com.samples.famodelabilitydevelop',
37                    moduleName: 'entry',
38                    abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton'
39                  };
40                  await featureAbility.startAbility({ want: want });
41                  hilog.info(domain, TAG, `Start ability succeed`);
42                }
43                catch (error) {
44                  hilog.error(domain, TAG, 'Start ability failed with ' + error);
45                }
46              })()
47            })
48          }
49          //...
50        }
51        //...
52      }
53      //...
54    }
55  }
56
57  ```
58
59- Sample code of **startAbility()** in the stage model:
60
61  ```ts
62  import { hilog } from '@kit.PerformanceAnalysisKit';
63  import { Want, common, Caller } from '@kit.AbilityKit';
64  import { BusinessError } from '@kit.BasicServicesKit';
65
66  const TAG: string = '[Page_UIAbilityComponentsInteractive]';
67  const DOMAIN_NUMBER: number = 0xFF00;
68
69  @Entry
70  @Component
71  struct Page_UIAbilityComponentsInteractive {
72    private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
73    caller: Caller | undefined = undefined;
74    build() {
75      Column() {
76        //...
77        List({ initialIndex: 0 }) {
78          ListItem() {
79            Row() {
80              //...
81            }
82            .onClick(() => {
83              // Context is a member of the ability object and is required for invoking inside a non-ability object.
84              // Pass in the Context object.
85              let wantInfo: Want = {
86                deviceId: '', // An empty deviceId indicates the local device.
87                bundleName: 'com.samples.stagemodelabilitydevelop',
88                moduleName: 'entry', // moduleName is optional.
89                abilityName: 'FuncAbilityA',
90                parameters: { // Custom information.
91                  info: 'From the UIAbilityComponentsInteractive page of EntryAbility',
92                },
93              };
94              // context is the UIAbilityContext of the initiator UIAbility.
95              this.context.startAbility(wantInfo).then(() => {
96                hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
97              }).catch((error: BusinessError) => {
98                hilog.error(DOMAIN_NUMBER, TAG, 'startAbility failed.');
99              });
100            })
101          }
102          //...
103        }
104        //...
105      }
106      //...
107    }
108  }
109  ```
110