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'; 12import hilog from '@ohos.hilog'; 13 14const TAG: string = 'PagePageAbilityFirst'; 15const domain: number = 0xFF00; 16``` 17```ts 18(async (): Promise<void> => { 19 let wantInfo: Want = { 20 bundleName: 'com.samples.famodelabilitydevelop', 21 abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton', 22 parameters: { page: 'pages/second' } 23 }; 24 featureAbility.startAbility({ want: wantInfo }).then((data) => { 25 hilog.debug(domain, TAG, `restartAbility success : ${data}`); 26 }); 27})() 28``` 29 30 31Obtain the **want** parameter that contains the page information from the **onNewWant()** callback of the target PageAbility. 32 33```ts 34// Construct a singleton object in GlobalContext.ts. 35export class GlobalContext { 36 private constructor() { 37 } 38 39 private static instance: GlobalContext; 40 private _objects = new Map<string, Object>(); 41 42 public static getContext(): GlobalContext { 43 if (!GlobalContext.instance) { 44 GlobalContext.instance = new GlobalContext(); 45 } 46 return GlobalContext.instance; 47 } 48 49 getObject(value: string): Object | undefined { 50 return this._objects.get(value); 51 } 52 53 setObject(key: string, objectClass: Object): void { 54 this._objects.set(key, objectClass); 55 } 56} 57``` 58 59```ts 60import Want from '@ohos.app.ability.Want'; 61import featureAbility from '@ohos.ability.featureAbility'; 62import { GlobalContext } from '../utils/GlobalContext'; 63 64class PageAbilitySingleton { 65 onNewWant(want: Want) { 66 featureAbility.getWant().then((want) => { 67 GlobalContext.getContext().setObject('newWant', want); 68 }) 69 } 70} 71 72export default new PageAbilitySingleton(); 73``` 74 75 76Obtain the **want** parameter that contains the page information from the custom component of the target PageAbility and process the route based on the URI. 77 78```ts 79import Want from '@ohos.app.ability.Want'; 80import router from '@ohos.router'; 81import { GlobalContext } from '../../utils/GlobalContext'; 82 83@Entry 84@Component 85struct First { 86 onPageShow() { 87 let newWant = GlobalContext.getContext().getObject('newWant') as Want; 88 if (newWant) { 89 if (newWant.parameters) { 90 if (newWant.parameters.page) { 91 router.pushUrl({ url: newWant.parameters.page as string}); 92 GlobalContext.getContext().setObject("newWant", undefined) 93 } 94 } 95 } 96 } 97 98 build() { 99 Column() { 100 Row() { 101 Text('singleton_first_title') 102 .fontSize(24) 103 .fontWeight(FontWeight.Bold) 104 .textAlign(TextAlign.Start) 105 .margin({ top: 12, bottom: 11, right: 24, left: 24 }) 106 } 107 .width('100%') 108 .height(56) 109 .justifyContent(FlexAlign.Start) 110 111 Image('pic_empty') 112 .width(120) 113 .height(120) 114 .margin({ top: 224 }) 115 116 Text('no_content') 117 .fontSize(14) 118 .margin({ top: 8, bottom: 317, right: 152, left: 152 }) 119 .fontColor('text_color') 120 .opacity(0.4) 121 } 122 .width('100%') 123 .height('100%') 124 .backgroundColor('backGrounding') 125 } 126} 127``` 128 129 130When 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.pushUrl** to start a specified page. 131 132 133When 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. 134 135```ts 136import featureAbility from '@ohos.ability.featureAbility'; 137import Want from '@ohos.app.ability.Want'; 138import { BusinessError } from '@ohos.base'; 139import fs from '@ohos.file.fs'; 140import promptAction from '@ohos.promptAction'; 141import worker from '@ohos.worker'; 142import hilog from '@ohos.hilog'; 143 144const TAG: string = 'PagePageAbilityFirst'; 145const domain: number = 0xFF00; 146 147@Entry 148@Component 149struct PagePageAbilityFirst { 150 build() { 151 Column() { 152 //... 153 List({ initialIndex: 0 }) { 154 //... 155 ListItem() { 156 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 157 //... 158 } 159 .onClick(() => { 160 let want: Want = { 161 bundleName: 'com.samples.famodelabilitydevelop', 162 abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard', 163 parameters: { page: 'pages/first' } 164 }; 165 featureAbility.startAbility({ want: want }).then((data) => { 166 hilog.info(domain, TAG, `startAbility finish:${data}`); 167 }).catch((err: BusinessError) => { 168 hilog.info(domain, TAG, `startAbility failed errcode:${err.code}`); 169 }) 170 }) 171 } 172 //... 173 ListItem() { 174 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 175 //... 176 } 177 .onClick(() => { 178 let want: Want = { 179 bundleName: 'com.samples.famodelabilitydevelop', 180 abilityName: 'com.samples.famodelabilitydevelop.PageAbilityStandard', 181 parameters: { page: 'pages/second' } 182 }; 183 featureAbility.startAbility({ want: want }).then((data) => { 184 hilog.info(domain, TAG, `startAbility finish:${data}`); 185 }).catch((err: BusinessError) => { 186 hilog.info(domain, TAG, `startAbility failed errcode:${err.code}`); 187 }) 188 }) 189 } 190 //... 191 } 192 //... 193 } 194 //... 195 } 196} 197``` 198 199 200In 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. 201 202```ts 203import featureAbility from '@ohos.ability.featureAbility'; 204import router from '@ohos.router'; 205 206class PageAbilityStandard { 207 onCreate() { 208 featureAbility.getWant().then((want) => { 209 if (want.parameters) { 210 if (want.parameters.page) { 211 router.pushUrl({ url: want.parameters.page as string }); 212 } 213 } 214 }) 215 } 216} 217 218export default new PageAbilityStandard(); 219``` 220