• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# PageAbility Switching
2
3
4The PageAbility component in the FA model corresponds to the UIAbility component in the stage model. To switch a PageAbility to a UIAbility, perform the following operations:
5
6
71. [Create a UIAbility](uiability-usage.md) in the stage model.
8
92. Migrate the PageAbility code to the UIAbility.
10
11The PageAbility lifecycle is basically the same as the UIAbility lifecycle. The table below describes the details.
12
13   | PageAbility| UIAbility| Mapping Description|
14   | -------- | -------- | -------- |
15   | onCreate(): void| onCreate(want: Want, param: AbilityConstant.LaunchParam): void | The two methods have the same meaning and invoking time. In the stage model, parameters are added to the callback so that you can obtain startup-related data during creation.|
16   | NA | onWindowStageCreate(windowStage: window.WindowStage): void| This method is available only in the stage model. The callback is invoked when a window is created.|
17   | onActive(): void | on(eventType: 'windowStageEvent', callback: Callback&lt;WindowStageEventType&gt;): void;<br>WindowStageEventType.ACTIVE | The two methods have the same meaning and invoking time. In the stage model, this method is moved to the window object.|
18   | onShow(): void | onForeground(): void | The two methods have the same meaning, invoking time, and parameters.|
19   | onNewWant(want: Want): void| onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void| The two methods have the same meaning and invoking time. In the stage model, the **LaunchParam** parameter is added to notify the application of the startup cause.|
20   | onInactive(): void| on(eventType: 'windowStageEvent', callback: Callback&lt;WindowStageEventType&gt;): void;<br>WindowStageEventType.INACTIVE | The two methods have the same meaning and invoking time. In the stage model, this method is moved to the window object.|
21   | onHide(): void | onBackground(): void | The two methods have the same meaning, invoking time, and parameters.|
22   | NA | onWindowStageDestroy(): void | This method is available only in the stage model. The callback is invoked when a window is destroyed.|
23| onDestroy(): void | onDestroy(): void | The two methods have the same meaning, invoking time, and parameters.|
24
25![pageability-switch](figures/pageability-switch.png)
26
273. Adjust the migrated code, since the methods of loading pages are different.
28
29   - In the FA model, you can configure the page to be loaded by setting page information in **config.json**.
30   - In the stage model, you must call **windowStage.loadContent** in the **onWindowStageCreate** callback to load a page.
31
32   For example, to load the **pages/Index** page after the ability is started, use the following code in the **config.json** file in the FA model:
33
34
35   ```json
36   "pages" : [
37       "pages/Index"
38   ]
39   ```
40
41   In the stage model, implement the following method in **MainAbility**:
42
43
44   ```ts
45   import Window from '@ohos.window'
46
47   onWindowStageCreate(windowStage: Window.WindowStage) {
48       // Main window is created. Set a main page for this ability.
49       windowStage.loadContent('pages/Index', (err, data) => {
50           if (err.code) {
51               console.error("loadContent failed")
52               return;
53           }
54       });
55   }
56   ```
57