• 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';
11import Want from '@ohos.app.ability.Want';
12
13async function restartAbility() {
14    let wantInfo: Want = {
15        bundleName: "com.sample.MyApplication",
16        abilityName: "EntryAbility",
17        parameters: {
18            page: "pages/second"
19        }
20    };
21    featureAbility.startAbility({
22        want: wantInfo
23    }).then((data) => {
24        console.info('restartAbility success.');
25    });
26}
27```
28
29
30Obtain the **want** parameter that contains the page information from the **onNewWant()** callback of the target PageAbility.
31
32```ts
33// Construct a singleton object in GlobalContext.ts.
34export class GlobalContext {
35  private constructor() {}
36  private static instance: GlobalContext;
37  private _objects = new Map<string, Object>();
38
39  public static getContext(): GlobalContext {
40    if (!GlobalContext.instance) {
41      GlobalContext.instance = new GlobalContext();
42    }
43    return GlobalContext.instance;
44  }
45
46  getObject(value: string): Object | undefined {
47    return this._objects.get(value);
48  }
49
50  setObject(key: string, objectClass: Object): void {
51    this._objects.set(key, objectClass);
52  }
53}
54```
55
56```ts
57import Want from '@ohos.application.Want';
58import { GlobalContext } from './GlobalContext';
59
60class EntryAbility {
61  onNewWant(want: Want) {
62    GlobalContext.getContext().setObject("newWant", want);
63  }
64}
65
66export default new EntryAbility()
67```
68
69
70Obtain the **want** parameter that contains the page information from the custom component of the target PageAbility and process the route based on the URI.
71
72```ts
73import Want from '@ohos.application.Want';
74import router from '@ohos.router';
75import { GlobalContext } from '../GlobalContext';
76
77@Entry
78@Component
79struct Index {
80  @State message: string = 'Router Page'
81
82  onPageShow() {
83    console.info('Index onPageShow')
84    let newWant = GlobalContext.getContext().getObject("newWant") as Want
85    if (newWant.parameters) {
86      if (newWant.parameters.page) {
87        router.push({ url: newWant.parameters.page });
88        GlobalContext.getContext().setObject("newWant", undefined)
89      }
90    }
91  }
92
93  build() {
94    Row() {
95      Column() {
96        Text(this.message)
97          .fontSize(50)
98          .fontWeight(FontWeight.Bold)
99      }
100      .width('100%')
101    }
102    .height('100%')
103  }
104}
105```
106
107
108When 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.
109
110
111When 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.
112
113```ts
114import featureAbility from '@ohos.ability.featureAbility';
115import { BusinessError } from '@ohos.base';
116
117@Entry
118@Component
119struct Index {
120  @State message: string = 'Hello World'
121
122  build() {
123    Row() {
124      Button("startAbility")
125        .onClick(() => {
126          featureAbility.startAbility({
127            want: {
128              bundleName: "com.exm.myapplication",
129              abilityName: "com.exm.myapplication.EntryAbility",
130              parameters: { page: "pages/page1" }
131            }
132          }).then((data) => {
133            console.info("startAbility finish");
134          }).catch((err: BusinessError) => {
135            console.info("startAbility failed errcode:" + err.code)
136          })
137        })
138      ...
139      Button("page2")
140        .onClick(() => {
141          featureAbility.startAbility({
142            want: {
143              bundleName: "com.exm.myapplication",
144              abilityName: "com.exm.myapplication.EntryAbility",
145              parameters: { page: "pages/page2" }
146            }
147          }).then((data) => {
148            console.info("startAbility finish");
149          }).catch((err: BusinessError) => {
150            console.info("startAbility failed errcode:" + err.code)
151          })
152        })
153      ...
154    }
155    ...
156  }
157}
158```
159
160
161In 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.
162
163```ts
164import featureAbility from '@ohos.ability.featureAbility';
165import router from '@ohos.router';
166
167class EntryAbility {
168  onCreate() {
169    featureAbility.getWant().then((want) => {
170      if (want.parameters) {
171        if (want.parameters.page) {
172          router.push({
173            url: want.parameters.page as string
174          })
175        }
176      }
177    })
178  }
179  onDestroy() {
180    // ...
181  }
182}
183
184export default new EntryAbility()
185```
186