1# Starting a PageAbility from the Stage Model
2
3
4This topic describes how the two application components of the stage model start the PageAbility component of the FA model.
5
6
7## UIAbility Starting a PageAbility
8
9A UIAbility starts a PageAbility in the same way as it starts another UIAbility.
10
11> **NOTE**
12>
13> In the FA model, **abilityName** consists of **bundleName** and **AbilityName**. For details, see the code snippet below.
14
15```ts
16import { common, Want } from '@kit.AbilityKit';
17import { hilog } from '@kit.PerformanceAnalysisKit';
18import { BusinessError } from '@kit.BasicServicesKit';
19
20const TAG: string = '[Page_StartFAModel]';
21const DOMAIN_NUMBER: number = 0xFF00;
22
23@Entry
24@Component
25struct Page_StartFAModel {
26  private context = getContext(this) as common.UIAbilityContext;
27
28  build() {
29    Column() {
30      //...
31      List({ initialIndex: 0 }) {
32        ListItem() {
33          Row() {
34            //...
35          }
36          .onClick(() => {
37            let want: Want = {
38              bundleName: 'com.samples.famodelabilitydevelop',
39              abilityName: 'com.samples.famodelabilitydevelop.MainAbility'
40            };
41            this.context.startAbility(want).then(() => {
42              hilog.info(DOMAIN_NUMBER, TAG, 'Start Ability successfully.');
43            }).catch((error: BusinessError) => {
44              hilog.error(DOMAIN_NUMBER, TAG, `Ability failed: ` + JSON.stringify(error));
45            });
46          })
47        }
48        //...
49      }
50      //...
51    }
52    //...
53  }
54}
55```
56
57
58## UIAbility Accessing a PageAbility (startAbilityForResult)
59
60Different from **startAbility()**, **startAbilityForResult()** obtains the execution result when the PageAbility is destroyed.
61
62A UIAbility starts a PageAbility through **startAbilityForResult()** in the same way as it starts another UIAbility.
63
64
65```ts
66import { common, Want } from '@kit.AbilityKit';
67import { hilog } from '@kit.PerformanceAnalysisKit';
68import { BusinessError } from '@kit.BasicServicesKit';
69import { promptAction } from '@kit.ArkUI';
70
71const TAG: string = '[Page_StartFAModel]';
72const DOMAIN_NUMBER: number = 0xFF00;
73
74@Entry
75@Component
76struct Page_StartFAModel {
77  private context = getContext(this) as common.UIAbilityContext;
78
79  build() {
80    Column() {
81      //...
82      List({ initialIndex: 0 }) {
83        ListItem() {
84          Row() {
85            //...
86          }
87          .onClick(() => {
88            let want: Want = {
89              bundleName: 'com.samples.famodelabilitydevelop',
90              abilityName: 'com.samples.famodelabilitydevelop.MainAbility',
91            };
92            this.context.startAbilityForResult(want).then((result) => {
93              hilog.info(DOMAIN_NUMBER, TAG, 'Ability verify result: ' + JSON.stringify(result));
94              if (result !== null) {
95                promptAction.showToast({
96                  message: JSON.stringify(result)
97                });
98              }
99            }).catch((error: BusinessError) => {
100              hilog.error(DOMAIN_NUMBER, TAG, `Ability failed: ` + JSON.stringify(error));
101            });
102          })
103        }
104        //...
105      }
106      //...
107    }
108    //...
109  }
110}
111```
112
113
114## ExtensionAbility Starting a PageAbility
115
116The following uses the ServiceExtensionAbility component as an example to describe how an ExtensionAbility starts a PageAbility. A ServiceExtensionAbility starts a PageAbility in the same way as it starts a UIAbility.
117
118
119```ts
120import { Want, ServiceExtensionAbility } from '@kit.AbilityKit';
121import { hilog } from '@kit.PerformanceAnalysisKit';
122import { BusinessError } from '@kit.BasicServicesKit';
123import { rpc } from '@kit.IPCKit';
124import ServiceExtImpl from '../IdlServiceExt/idl_service_ext_impl';
125
126const TAG: string = '[ServiceExtAbility]';
127const DOMAIN_NUMBER: number = 0xFF00;
128
129export default class ServiceExtAbility extends ServiceExtensionAbility {
130  serviceExtImpl: ServiceExtImpl = new ServiceExtImpl('ExtImpl');
131
132  onCreate(want: Want): void {
133    let serviceExtensionContext = this.context;
134    hilog.info(DOMAIN_NUMBER, TAG, `onCreate, want: ${want.abilityName}`);
135  };
136
137  onRequest(want: Want, startId: number): void {
138    hilog.info(DOMAIN_NUMBER, TAG, `onRequest, want: ${want.abilityName}`);
139    if (want.parameters?.key === 'ConnectFaPageAbility') {
140      let wantFA: Want = {
141        bundleName: 'com.samples.famodelabilitydevelop',
142        abilityName: 'com.samples.famodelabilitydevelop.MainAbility',
143      };
144      this.context.startAbility(wantFA).then(() => {
145        hilog.info(DOMAIN_NUMBER, TAG, 'Start Ability successfully.');
146      }).catch((error: BusinessError) => {
147        hilog.info(DOMAIN_NUMBER, TAG, `Ability failed: ${JSON.stringify(error)}`);
148      });
149    }
150  };
151
152  onConnect(want: Want): rpc.RemoteObject {
153    hilog.info(DOMAIN_NUMBER, TAG, `onConnect, want: ${want.abilityName}`);
154    // Return the ServiceExtImpl object, through which the client can communicate with the ServiceExtensionAbility.
155    return this.serviceExtImpl as rpc.RemoteObject;
156  };
157
158  onDisconnect(want: Want): void {
159    hilog.info(DOMAIN_NUMBER, TAG, `onDisconnect, want: ${want.abilityName}`);
160  };
161
162  onDestroy(): void {
163    hilog.info(DOMAIN_NUMBER, TAG, 'onDestroy');
164  };
165}
166```
167