• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using Explicit Want to Start an Ability
2
3
4When a user touches a button in an application, the application often needs to start a UIAbility component to complete a specific task. The following describes how to use explicit Want to start a UIAbility component in an application.
5
6
7## How to Develop
8
91. In a project of the stage model, create an ability and a page, named **callerAbility** and **Index.ets**, respectively. Use the **windowStage.loadContent()** method in the **onWindowStageCreate** function in the **callerAbility.ts** file to bind the two.
10
11   ```ts
12   // ...
13       // callerAbility.ts
14       onWindowStageCreate(windowStage) {
15           // Main window is created, set main page for this ability
16           console.info('[Demo] EntryAbility onWindowStageCreate')
17           // Bind callerAbility with a paged named Index
18           windowStage.loadContent('pages/Index')
19       }
20   // ...
21   ```
22
232. Repeat the preceding operation to create another ability named **calleeAbility**.
24
253. Add a button to the **Index.ets** page of **callerAbility**.
26
27   ```ts
28   // ...
29     build() {
30       Row() {
31         Column() {
32           Text('hello')
33           .fontSize(50)
34           .fontWeight(FontWeight.Bold)
35           // A new button with will call explicitStartAbility() when clicked.
36           Button("CLICKME")
37           .onClick(this.explicitStartAbility) // For details about explicitStartAbility, see the sample code below.
38           // ...
39         }
40         .width('100%')
41       }
42       .height('100%')
43     }
44   // ...
45   ```
46
474. Override the **onClick** method and use explicit Want to start **calleeAbility** in the method. The **bundleName** field can be obtained from the **AppScope > app.json5** file of the project. The **abilityName** field can be obtained from the **yourModuleName > src > main > module.json5** file of the corresponding module.
48
49   ```ts
50   import common from '@ohos.app.ability.common';
51
52   // ...
53     async explicitStartAbility() {
54       try {
55         // Explicit want with abilityName specified.
56         let want = {
57           deviceId: "",
58           bundleName: "com.example.myapplication",
59           abilityName: "calleeAbility"
60         };
61         let context = getContext(this) as common.UIAbilityContext;
62         await context.startAbility(want);
63         console.info(`explicit start ability succeed`);
64       } catch (error) {
65         console.info(`explicit start ability failed with ${error.code}`);
66       }
67     }
68   // ...
69   ```
70
715. When you touch **CLICKME**, the corresponding page is displayed.
72
73   <img src="figures/startAbilityWtExplicitWant.PNG" alt="startAbilityWtExplicitWant" style="zoom: 80%;" />
74