1# PageAbility切换
2
3
4FA模型中PageAbility对应Stage模型中的UIAbility,PageAbility切换为UIAbility的方法如下。
5
6
71. 在Stage应用中[创建UIAbility](uiability-usage.md)。
8
92. 将FA应用中PageAbility的代码迁移到新创建的UIAbility中。
10   FA应用中PageAbility和Stage应用中的UIAbility生命周期基本一致,两者的生命周期详细对比见下表。
11
12   | FA的PageAbility | Stage的UIAbility | 对应关系描述 |
13   | -------- | -------- | -------- |
14   | onCreate(): void | onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void | 两者的意义和调用时机一致,Stage模型在回调中新增了参数,方便开发者在创建的时候获取启动相关的数据。 |
15   | NA | onWindowStageCreate(windowStage: window.WindowStage): void | Stage模型新增,窗口创建时由系统回调。 |
16   | onActive():&nbsp;void | on(eventType:&nbsp;'windowStageEvent',&nbsp;callback:&nbsp;Callback&lt;WindowStageEventType&gt;):&nbsp;void;<br/>WindowStageEventType.ACTIVE | 两者的意义和调用时机一致。Stage模型下移动到了窗口对象中。 |
17   | onShow():&nbsp;void | onForeground():&nbsp;void | 两者的意义和调用时机一致,参数也一致。 |
18   | onNewWant(want:&nbsp;Want):&nbsp;void | onNewWant(want:&nbsp;Want,&nbsp;launchParam:&nbsp;AbilityConstant.LaunchParam):&nbsp;void | 两者的意义和调用时机一致,Stage模型多了LaunchParam参数来告知应用启动原因。 |
19   | onInactive():&nbsp;void | on(eventType:&nbsp;'windowStageEvent',&nbsp;callback:&nbsp;Callback&lt;WindowStageEventType&gt;):&nbsp;void;<br/>WindowStageEventType.INACTIVE | 两者的意义和调用时机一致。Stage模型下移动到了窗口对象中。 |
20   | onHide():&nbsp;void | onBackground():&nbsp;void | 两者的意义和调用时机一致,参数也一致。 |
21   | NA | onWindowStageDestroy():&nbsp;void | Stage模型新增,窗口销毁时由系统回调。 |
22   | onDestroy():&nbsp;void | onDestroy():&nbsp;void | 两者的意义和调用时机一致,参数也一致。 |
23
24   ![pageability-switch](figures/pageability-switch.png)
25
263. 对迁移过来的代码进行调整,主要有以下两部分。
27
28    1. 指定加载页面的方式不同。
29
30        - 在FA模型中,通过在config.json中设置页面信息来配置需要加载的页面。
31        - 在Stage模型中,则是通过在onWindowStageCreate回调中调用windowStage.loadContent实现对页面的加载。
32
33        例如,开发者希望Ability启动后加载"pages/Index"页面,在FA模型中,开发者需要在config.json中加入如下代码:
34
35
36        ```json
37        "pages" : [
38            "pages/Index"
39        ]
40        ```
41
42        在Stage模型中,则在MainAbility中实现如下接口:
43
44
45        ```ts
46        import { UIAbility } from '@kit.AbilityKit';
47        import { hilog } from '@kit.PerformanceAnalysisKit';
48        import { window } from '@kit.ArkUI';
49
50        export default class TestAbility extends UIAbility {
51          // ...
52          onWindowStageCreate(windowStage: window.WindowStage) {
53            hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate');
54            windowStage.loadContent('testability/pages/Index', (err, data) => {
55              if (err.code) {
56                hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
57                return;
58              }
59              hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s',
60                JSON.stringify(data) ?? '');
61            });
62          }
63          // ...
64        }
65        ```
66
67    2. 在resources/base/profile/main_pages.json中配置页面,以"pages/Index"为例:
68        ```json
69        {
70          "src": [
71            "pages/Index"
72          ]
73        }
74        ```