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