• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# UIAbility Launch Type
2
3
4The launch type of the UIAbility component refers to the state of the UIAbility instance at startup. Three launch types are available:
5
6
7- [Singleton](#singleton)
8
9- [Multiton](#multiton)
10
11- [Specified](#specified)
12
13
14## Singleton
15
16**singleton** is the default launch type.
17
18Each time [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called, if a UIAbility instance of this type already exists in the application process, the instance is reused. In other words, UIAbility of this type can have only one instance in the system, meaning that only one mission is displayed in the system application Recents.
19
20**Figure 1** Demonstration effect in singleton mode
21
22![uiability-launch-type1](figures/uiability-launch-type1.gif)
23
24> **NOTE**
25>
26> If [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called to start an existing UIAbility instance in singleton mode, that instance is started, and no new UIAbility instance is created. In this case, the [onNewWant()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonnewwant) callback is invoked, but the [onCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityoncreate) and [onWindowStageCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) callbacks are not.
27
28To use the singleton mode, set **launchType** in the [module.json5 file](../quick-start/module-configuration-file.md) to **singleton**.
29
30
31```json
32{
33  "module": {
34    ...
35    "abilities": [
36      {
37        "launchType": "singleton",
38        ...
39      }
40    ]
41  }
42}
43```
44
45
46## Multiton
47
48In multiton mode, each time [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called, a new UIAbility instance is created in the application process. Multiple missions are displayed for UIAbility of this type in Recents.
49
50**Figure 2** Demonstration effect in multiton mode
51
52![uiability-launch-type2](figures/uiability-launch-type2.gif)
53
54To use the multiton mode, set **launchType** in the [module.json5 file](../quick-start/module-configuration-file.md) to **multiton**.
55
56
57```json
58{
59  "module": {
60    ...
61    "abilities": [
62      {
63        "launchType": "multiton",
64        ...
65      }
66    ]
67  }
68}
69```
70
71
72## Specified
73
74The **specified** mode is used in some special scenarios. For example, in a document application, you may want a document instance to be created each time you create a document, and you may also want to use the same document instance when you open an existing document.
75
76**Figure 3** Demonstration effect in specified mode
77
78![uiability-launch-type3](figures/uiability-launch-type3.gif)
79
80In the following example, there are two UIAbility components: EntryAbility and SpecifiedAbility (with the launch type **specified**). To start SpecifiedAbility from EntryAbility, proceed as follows:
81
821. In SpecifiedAbility, set **launchType** in the [module.json5 file](../quick-start/module-configuration-file.md) to **specified**.
83
84   ```json
85   {
86     "module": {
87       ...
88       "abilities": [
89         {
90           "launchType": "specified",
91           ...
92         }
93       ]
94     }
95   }
96   ```
97
982. Create a unique string key for the SpecifiedAbility instance. Each time [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called, the application, based on the key, identifies the UIAbility instance used to respond to the request. In EntryAbility, add a custom parameter, for example, **instanceKey**, to the **want** parameter in [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to distinguish the UIAbility instances.
99
100   ```ts
101   // Configure a unique key for each UIAbility instance.
102   // For example, in the document usage scenario, use the document path as the key.
103   import common from '@ohos.app.ability.common';
104   import Want from '@ohos.app.ability.Want';
105   import { BusinessError } from '@ohos.base';
106
107   function getInstance() {
108     return 'key';
109   }
110
111   let context:common.UIAbilityContext = ...; // context is the UIAbilityContext of the initiator UIAbility.
112   let want: Want = {
113     deviceId: '', // An empty deviceId indicates the local device.
114     bundleName: 'com.example.myapplication',
115     abilityName: 'SpecifiedAbility',
116     moduleName: 'specified', // moduleName is optional.
117     parameters: { // Custom information.
118       instanceKey: getInstance(),
119     },
120   }
121
122   context.startAbility(want).then(() => {
123     console.info('Succeeded in starting ability.');
124   }).catch((err: BusinessError) => {
125     console.error(`Failed to start ability. Code is ${err.code}, message is ${err.message}`);
126   })
127   ```
128
1293. Before SpecifiedAbility is started, the [onAcceptWant()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback of the corresponding AbilityStage instance is invoked to obtain the key of the target UIAbility. If a UIAbility instance matching the key exists, the system starts the UIAbility instance and invokes its [onNewWant()](../reference/apis/js-apis-app-ability-uiAbility.md#abilityonnewwant) callback. Otherwise, the system creates a new UIAbility instance and invokes its [onCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityoncreate) and [onWindowStageCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) callbacks.
130
131   In the sample code, the [onAcceptWant()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback uses the passed **want** parameter to obtain the custom parameter **instanceKey**. The service logic returns a key string based on the **instanceKey** parameter to identify the UIAbility instance. If the returned key maps to a started UIAbility instance, the system pulls the UIAbility instance back to the foreground and gives it the focus. If the returned key does not map to a started UIAbility instance, the system creates a new UIAbility instance and starts it.
132
133   ```ts
134   import AbilityStage from '@ohos.app.ability.AbilityStage';
135   import Want from '@ohos.app.ability.Want';
136
137   export default class MyAbilityStage extends AbilityStage {
138     onAcceptWant(want: Want): string {
139       // In the AbilityStage instance of the callee, a key string corresponding to a UIAbility instance is returned for UIAbility whose launch type is specified.
140       // In this example, SpecifiedAbility of module1 is returned.
141       if (want.abilityName === 'SpecifiedAbility') {
142         // The returned key string is a custom string.
143         if (want.parameters) {
144           return `SpecifiedAbilityInstance_${want.parameters.instanceKey}`;
145         }
146       }
147
148       return '';
149     }
150   }
151   ```
152
153   > **NOTE**
154   >
155   > - If [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called to start an existing UIAbility instance in specified mode, and the [onAcceptWant()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback of [AbilityStage](../reference/apis/js-apis-app-ability-abilityStage.md) matches that UIAbility instance, that instance is started, and no new UIAbility instance is created. In this case, the [onNewWant()](../reference/apis/js-apis-app-ability-uiAbility.md#abilityonnewwant) callback is invoked, but the [onCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityoncreate) and [onWindowStageCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) callbacks are not.
156   >
157   > - AbilityStage is not automatically generated by default in the project of DevEco Studio. For details about how to create an AbilityStage file, see [AbilityStage Component Container](abilitystage.md).
158
159
160For example, in the document application, different keys are bound to different document instances. Each time a document is created, a new key (for example, file path) is passed in, and a new UIAbility instance is created when UIAbility is started in AbilityStage. However, when an existing document is opened, the same UIAbility instance is started again in AbilityStage.
161
162The following steps are used as an example.
163
164   1. Open file A. A UIAbility instance, UIAbility instance 1, is started.
165   2. Close the process of file A in Recents. UIAbility instance 1 is destroyed. Return to the home screen and open file A again. A new UIAbility instance, UIAbility instance 2, is started.
166   3. Return to the home screen and open file B. A new UIAbility instance, UIAbility instance 3, is started.
167   4. Return to the home screen and open file A again. UIAbility instance 2 is started. This is because the system automatically matches the key with the UIAbility instance and starts the UIAbility instance that has a matching key. In this example, UIAbility instance 2 has the same key as file A. Therefore, the system pulls back UIAbility instance 2 and focuses it without creating a new instance.
168