• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = this.getUIContext().getHostContext() 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';
69
70const TAG: string = '[Page_StartFAModel]';
71const DOMAIN_NUMBER: number = 0xFF00;
72
73@Entry
74@Component
75struct Page_StartFAModel {
76  private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
77
78  build() {
79    Column() {
80      //...
81      List({ initialIndex: 0 }) {
82        ListItem() {
83          Row() {
84            //...
85          }
86          .onClick(() => {
87            let want: Want = {
88              bundleName: 'com.samples.famodelabilitydevelop',
89              abilityName: 'com.samples.famodelabilitydevelop.MainAbility',
90            };
91            this.context.startAbilityForResult(want).then((result) => {
92              hilog.info(DOMAIN_NUMBER, TAG, 'Ability verify result: ' + JSON.stringify(result));
93              if (result !== null) {
94                this.getUIContext().getPromptAction().showToast({
95                  message: JSON.stringify(result)
96                });
97              }
98            }).catch((error: BusinessError) => {
99              hilog.error(DOMAIN_NUMBER, TAG, `Ability failed: ` + JSON.stringify(error));
100            });
101          })
102        }
103        //...
104      }
105      //...
106    }
107    //...
108  }
109}
110```
111
112
113## ExtensionAbility Starting a PageAbility
114
115The 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.
116
117
118```ts
119import { Want, ServiceExtensionAbility } from '@kit.AbilityKit';
120import { hilog } from '@kit.PerformanceAnalysisKit';
121import { BusinessError } from '@kit.BasicServicesKit';
122import { rpc } from '@kit.IPCKit';
123import ServiceExtImpl from '../IdlServiceExt/idl_service_ext_impl';
124
125const TAG: string = '[ServiceExtAbility]';
126const DOMAIN_NUMBER: number = 0xFF00;
127
128export default class ServiceExtAbility extends ServiceExtensionAbility {
129  serviceExtImpl: ServiceExtImpl = new ServiceExtImpl('ExtImpl');
130
131  onCreate(want: Want): void {
132    let serviceExtensionContext = this.context;
133    hilog.info(DOMAIN_NUMBER, TAG, `onCreate, want: ${want.abilityName}`);
134  };
135
136  onRequest(want: Want, startId: number): void {
137    hilog.info(DOMAIN_NUMBER, TAG, `onRequest, want: ${want.abilityName}`);
138    if (want.parameters?.key === 'ConnectFaPageAbility') {
139      let wantFA: Want = {
140        bundleName: 'com.samples.famodelabilitydevelop',
141        abilityName: 'com.samples.famodelabilitydevelop.MainAbility',
142      };
143      this.context.startAbility(wantFA).then(() => {
144        hilog.info(DOMAIN_NUMBER, TAG, 'Start Ability successfully.');
145      }).catch((error: BusinessError) => {
146        hilog.error(DOMAIN_NUMBER, TAG, `Ability failed: ${JSON.stringify(error)}`);
147      });
148    }
149  };
150
151  onConnect(want: Want): rpc.RemoteObject {
152    hilog.info(DOMAIN_NUMBER, TAG, `onConnect, want: ${want.abilityName}`);
153    // Return the ServiceExtImpl object, through which the client can communicate with the ServiceExtensionAbility.
154    return this.serviceExtImpl as rpc.RemoteObject;
155  };
156
157  onDisconnect(want: Want): void {
158    hilog.info(DOMAIN_NUMBER, TAG, `onDisconnect, want: ${want.abilityName}`);
159  };
160
161  onDestroy(): void {
162    hilog.info(DOMAIN_NUMBER, TAG, 'onDestroy');
163  };
164}
165```
166