• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Stage模型启动FA模型PageAbility
2
3
4本小节介绍Stage模型的两种应用组件如何启动FA模型的PageAbility组件。
5
6
7## UIAbility启动PageAbility
8
9  UIAbility启动PageAbility和UIAbility启动UIAbility的方式完全相同。
10
11```ts
12import UIAbility from '@ohos.app.ability.UIAbility';
13
14export default class EntryAbility extends UIAbility {
15    onCreate(want, launchParam) {
16        console.info("EntryAbility onCreate")
17    }
18    onDestroy() {
19        console.info("EntryAbility onDestroy")
20    }
21    onWindowStageCreate(windowStage) {
22        console.info("EntryAbility onWindowStageCreate")
23        windowStage.loadContent('pages/Index', (err, data) => {
24            ...
25        });
26        let want = {
27            bundleName: "com.ohos.fa",
28            abilityName: "EntryAbility",
29        };
30        this.context.startAbility(want).then(() => {
31            console.info('Start Ability successfully.');
32        }).catch((error) => {
33            console.error("Ability failed: " + JSON.stringify(error));
34        });
35    }
36    onWindowStageDestroy() {
37        console.info("EntryAbility onWindowStageDestroy")
38    }
39    onForeground() {
40        console.info("EntryAbility onForeground")
41    }
42    onBackground() {
43        console.info("EntryAbility onBackground")
44    }
45}
46```
47
48
49## UIAbility访问PageAbility(startAbilityForResult)
50
51startAbilityForResult和startAbility的区别是当PageAbility销毁的时候会返回执行结果。
52
53UIAbility通过startAbilityForResult启动PageABility和UIAbility通过startAbilityForResult启动UIAbility的代码一样,没有任何区别。
54
55
56```ts
57import UIAbility from '@ohos.app.ability.UIAbility';
58
59export default class EntryAbility extends UIAbility {
60    onCreate(want, launchParam) {
61        console.info("EntryAbility onCreate")
62    }
63    onDestroy() {
64        console.info("EntryAbility onDestroy")
65    }
66    onWindowStageCreate(windowStage) {
67        console.info("EntryAbility onWindowStageCreate")
68        windowStage.loadContent('pages/Index', (err, data) => {
69            ...
70        });
71        let want = {
72            bundleName: "com.ohos.fa",
73            abilityName: "EntryAbility",
74        };
75        this.context.startAbilityForResult(want).then((result) => {
76            console.info('Ability verify result: ' + JSON.stringify(result));
77        }).catch((error) => {
78            console.error("Ability failed: " + JSON.stringify(error));
79        });
80    }
81    onWindowStageDestroy() {
82        console.info("EntryAbility onWindowStageDestroy")
83    }
84    onForeground() {
85        console.info("EntryAbility onForeground")
86    }
87    onBackground() {
88        console.info("EntryAbility onBackground")
89    }
90}
91```
92
93
94## ExtensionAbility启动PageAbility
95
96下面以ServiceExtensionAbility为例来说明ExtensionAbility启动PageAbility。ServiceExtensionAbility启动PageAbility和ServiceExtensionAbility启动UIAbility的方式完全相同。
97
98
99```ts
100import Extension from '@ohos.app.ability.ServiceExtensionAbility'
101
102export default class ServiceExtension extends Extension {
103    onCreate(want) {
104        console.info("ServiceExtension onCreate")
105    }
106    onDestroy() {
107        console.info("ServiceExtension onDestroy")
108    }
109    onRequest(want, startId) {
110        console.info("ServiceExtension onRequest")
111        let wantFA = {
112            bundleName: "com.ohos.fa",
113            abilityName: "EntryAbility",
114        };
115        this.context.startAbility(wantFA).then(() => {
116            console.info('Start Ability successfully.');
117        }).catch((error) => {
118            console.error("Ability failed: " + JSON.stringify(error));
119        });
120    }
121}
122```
123