start-page.md 5.3 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10
# 启动指定页面


当PageAbility的启动模式设置为单例时(具体设置方法和典型场景示例见[PageAbility的启动模式](pageability-launch-type.md),缺省情况下是单实例模式),若PageAbility已被拉起,再次启动PageAbility会触发onNewWant回调(即非首次拉起)。应用开发者可以通过want传递启动参数,例如开发者希望指定页面启动PageAbility,可以通过want中的parameters参数传递pages信息,具体示例代码如下:


调用方PageAbility的app.ets中或者page中,使用startAbility再次拉起PageAbility,通过want中的uri参数传递页面信息:

```ts
import featureAbility from '@ohos.ability.featureAbility';
L
liuliu 已提交
11
import Want from '@ohos.app.ability.Want';
Z
zengyawen 已提交
12 13

async function restartAbility() {
L
liuliu 已提交
14
    let wantInfo: Want = {
Z
zengyawen 已提交
15
        bundleName: "com.sample.MyApplication",
16
        abilityName: "EntryAbility",
Z
zengyawen 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
        parameters: {
            page: "pages/second"
        }
    };
    featureAbility.startAbility({
        want: wantInfo
    }).then((data) => {
        console.info('restartAbility success.');
    });
}
```


在目标端PageAbility的onNewWant回调中获取包含页面信息的want参数:

```ts
L
liuliu 已提交
33
// GlobalContext.ts 构造单例对象
L
update  
liuliu 已提交
34 35 36 37 38 39 40 41
export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
Z
zengyawen 已提交
42
    }
L
update  
liuliu 已提交
43 44 45 46 47 48 49 50 51 52 53
    return GlobalContext.instance;
  }

  getObject(value: string): Object | undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}
L
liuliu 已提交
54 55 56 57 58
```

```ts
import Want from '@ohos.application.Want';
import { GlobalContext } from './GlobalContext';
L
update  
liuliu 已提交
59 60 61 62 63

export default class EntryAbility{  
  onNewWant(want: Want) { 
    GlobalContext.getContext().setObject("newWant", want);  
  }
Z
zengyawen 已提交
64 65 66 67 68 69 70
}
```


在目标端页面的自定义组件中获取包含页面信息的want参数并根据uri做路由处理:

```ts
L
liuliu 已提交
71
import Want from '@ohos.application.Want';
L
update  
liuliu 已提交
72
import router from '@ohos.router';
L
liuliu 已提交
73
import { GlobalContext } from '../GlobalContext';
L
update  
liuliu 已提交
74

Z
zengyawen 已提交
75 76 77 78
@Entry
@Component
struct Index {
  @State message: string = 'Router Page'
L
liuliu 已提交
79
  
Z
zengyawen 已提交
80 81
  onPageShow() {
    console.info('Index onPageShow')
L
liuliu 已提交
82 83 84 85 86 87
    let newWant = GlobalContext.getContext().getObject("newWant") as Want
    if (newWant.parameters) {
      if (newWant.parameters.page) {
        router.push({ url: newWant.parameters.page });
        GlobalContext.getContext().setObject("newWant", undefined)
      }
Z
zengyawen 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    }
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}
```


106
当PageAbility的启动模式设置为多实例模式或为首次启动单例模式的PageAbility时(具体设置方法和典型场景示例见[PageAbility的启动模式](pageability-launch-type.md)),在调用方PageAbility中,通过want中的parameters参数传递要启动的指定页面的pages信息,调用startAbility()方法启动PageAbility。被调用方可以在onCreate中使用featureAbility的getWant方法获取want,再通过调用router.push实现启动指定页面。
Z
zengyawen 已提交
107 108 109 110 111


调用方的页面中实现按钮点击触发startAbility方法启动目标端PageAbility,startAbility方法的入参want中携带指定页面信息,示例代码如下:

```ts
L
update  
liuliu 已提交
112 113 114
import featureAbility from '@ohos.ability.featureAbility';
import { BusinessError } from '@ohos.base';

Z
zengyawen 已提交
115 116 117 118 119 120
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
L
liuliu 已提交
121 122 123 124 125 126 127 128 129 130 131
    Row() {
      Button("startAbility")
        .onClick(() => {
          featureAbility.startAbility({
            want: {
              bundleName: "com.exm.myapplication",
              abilityName: "com.exm.myapplication.EntryAbility",
              parameters: { page: "pages/page1" }
            }
          }).then((data) => {
            console.info("startAbility finish");
L
update  
liuliu 已提交
132
          }).catch((err: BusinessError) => {
L
liuliu 已提交
133 134
            console.info("startAbility failed errcode:" + err.code)
          })
Z
zengyawen 已提交
135
        })
L
liuliu 已提交
136 137 138 139 140 141 142 143 144 145 146
      ...
      Button("page2")
        .onClick(() => {
          featureAbility.startAbility({
            want: {
              bundleName: "com.exm.myapplication",
              abilityName: "com.exm.myapplication.EntryAbility",
              parameters: { page: "pages/page2" }
            }
          }).then((data) => {
            console.info("startAbility finish");
L
update  
liuliu 已提交
147
          }).catch((err: BusinessError) => {
L
liuliu 已提交
148 149
            console.info("startAbility failed errcode:" + err.code)
          })
Z
zengyawen 已提交
150
        })
L
liuliu 已提交
151 152
      ...
    }
153
    ...
Z
zengyawen 已提交
154 155 156 157 158
  }
}
```


G
geng-wenguang 已提交
159
目标端PageAbility的onCreate生命周期回调中通过featureAbility的getWant方法获取want,并对参数进行解析,实现指定页面拉起:
Z
zengyawen 已提交
160 161 162 163 164

```ts
import featureAbility from '@ohos.ability.featureAbility';
import router from '@ohos.router';

L
update  
liuliu 已提交
165
export default class EntryAbility {
Z
zengyawen 已提交
166 167
  onCreate() {
    featureAbility.getWant().then((want) => {
L
update  
liuliu 已提交
168 169 170
      if (want.parameters) {
        if (want.parameters.page) {
          router.push({
L
liuliu 已提交
171
            url: want.parameters.page as string
L
update  
liuliu 已提交
172 173
          })
        }
Z
zengyawen 已提交
174 175
      }
    })
L
update  
liuliu 已提交
176
  };
Z
zengyawen 已提交
177
  onDestroy() {
L
liuliu 已提交
178
    // ...
L
update  
liuliu 已提交
179
  };
Z
zengyawen 已提交
180 181
}
```