• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Starting a Specified Page
2
3
4When the launch type of a PageAbility is set to **singleton** (default), the **onNewWant()** callback is triggered if the PageAbility is not started for the first time. For details about the launch type, see [PageAbility Launch Type](pageability-launch-type.md). In this case, you can use the **want** parameter to transfer startup information. For example, if you want to start a PageAbility with a specified page, pass the pages information in **parameters** of **want**.
5
6
7In **app.ets** or **page** of the caller PageAbility, use **startAbility()** to start the PageAbility again, with the page information passed in the **uri** parameter in **want**.
8
9```ts
10import featureAbility from '@ohos.ability.featureAbility';
11
12async function restartAbility() {
13    let wantInfo = {
14        bundleName: "com.sample.MyApplication",
15        abilityName: "MainAbility",
16        parameters: {
17            page: "pages/second"
18        }
19    };
20    featureAbility.startAbility({
21        want: wantInfo
22    }).then((data) => {
23        console.info('restartAbility success.');
24    });
25}
26```
27
28
29Obtain the **want** parameter that contains the page information from the **onNewWant()** callback of the target PageAbility.
30
31```ts
32export default {
33    onNewWant(want) {
34        globalThis.newWant = want
35    }
36}
37```
38
39
40Obtain the **want** parameter that contains the page information from the custom component of the target PageAbility and process the route based on the URI.
41
42```ts
43import router from '@ohos.router'
44@Entry
45@Component
46struct Index {
47  @State message: string = 'Router Page'
48  newWant = undefined
49  onPageShow() {
50    console.info('Index onPageShow')
51    let newWant = globalThis.newWant
52    if (newWant.hasOwnProperty("page")) {
53      router.push({ url: newWant.page });
54      globalThis.newWant = undefined
55    }
56  }
57
58  build() {
59    Row() {
60      Column() {
61        Text(this.message)
62          .fontSize(50)
63          .fontWeight(FontWeight.Bold)
64      }
65      .width('100%')
66    }
67    .height('100%')
68  }
69}
70```
71
72
73When a PageAbility in multiton mode is started or when the PageAbility in singleton mode is started for the first time, you can use the **parameters** parameter in **want** to transfer the pages information and use the **startAbility()** method to start the PageAbility. For details about the launch type, see [PageAbility Launch Type](pageability-launch-type.md). The target PageAbility can use the **featureAbility.getWant()** method in **onCreate** to obtain the **want** parameter, and then call **router.push** to start a specified page.
74
75
76When a user touches the button on the page of the caller PageAbility, the **startAbility()** method is called to start the target PageAbility. The **want** parameter in **startAbility()** carries the specified page information.
77
78```ts
79import featureAbility from '@ohos.ability.featureAbility'
80@Entry
81@Component
82struct Index {
83  @State message: string = 'Hello World'
84
85  build() {
86    // ...
87    Button("startAbility")
88      .onClick(() => {
89        featureAbility.startAbility({
90          want: {
91            bundleName: "com.exm.myapplication",
92            abilityName: "com.exm.myapplication.MainAbility",
93            parameters: { page: "pages/page1" }
94          }
95        }).then((data) => {
96          console.info("startAbility finish");
97        }).catch((err) => {
98          console.info("startAbility failed errcode:" + err.code)
99        })
100      })
101    // ...
102    Button("page2")
103      .onClick(() => {
104        featureAbility.startAbility({
105          want: {
106            bundleName: "com.exm.myapplication",
107            abilityName: "com.exm.myapplication.MainAbility",
108            parameters: { page: "pages/page2" }
109          }
110        }).then((data) => {
111          console.info("startAbility finish");
112        }).catch((err) => {
113          console.info("startAbility failed errcode:" + err.code)
114        })
115      })
116    // ...
117  }
118}
119```
120
121
122In the **onCreate()** callback of the target PageAbility, use the **featureAbility.getWant()** method to obtain the **want** parameter, parse the parameter, and start the specified page.
123
124```ts
125import featureAbility from '@ohos.ability.featureAbility';
126import router from '@ohos.router';
127
128export default {
129  onCreate() {
130    featureAbility.getWant().then((want) => {
131      if (want.parameters.page) {
132        router.push({
133          url: want.parameters.page
134        })
135      }
136    })
137  },
138  onDestroy() {
139    // ...
140  },
141}
142```
143