• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# UIAbility Component Launch Type
2
3
4The launch type of the UIAbility component refers to the state of the UIAbility instance at startup. The system provides three launch types:
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. Therefore, only one UIAbility instance of this type exists in the system, that is, displayed in **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> Assume that the application already has a UIAbility instance created, and the launch type of the UIAbility instance is set to **singleton**. If [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called again to start the UIAbility instance, the original UIAbility 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.
27
28To use the singleton mode, set **launchType** in the [module.json5 configuration 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 of this type is created in the application process. Multiple UIAbility instances of this type are displayed 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 want a document instance to be created each time you create a document, but you want to use the same document instance when you repeatedly open an existing document.
75
76**Figure 3** Demonstration effect in specified mode
77
78![uiability-launch-type3](figures/uiability-launch-type3.gif)
79
80For example, there are two UIAbility components: EntryAbility and SpecifiedAbility (with the launch type **specified**). You are required to start SpecifiedAbility from EntryAbility.
81
821. In SpecifiedAbility, set the **launchType** field 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 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 instance.
99
100   ```ts
101   // Configure an independent key for each UIAbility instance.
102   // For example, in the document usage scenario, use the document path as the key.
103   function getInstance() {
104     ...
105   }
106
107   let context =...; // context is the UIAbilityContext of the initiator UIAbility.
108   let want = {
109     deviceId: '', // An empty deviceId indicates the local device.
110     bundleName: 'com.example.myapplication',
111     abilityName: 'SpecifiedAbility',
112     moduleName: 'specified', // moduleName is optional.
113     parameters: {// Custom information.
114       instanceKey: getInstance(),
115     },
116   }
117
118   context.startAbility(want).then(() => {
119     console.info('Succeeded in starting ability.');
120   }).catch((err) => {
121     console.error(`Failed to start ability. Code is ${err.code}, message is ${err.message}`);
122   })
123   ```
124
1253. 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 UIAbility, because the launch type of SpecifiedAbility is set to **specified**. 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.
126
127   In the sample code, the [onAcceptWant()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback parses the **want** parameter to obtain the custom parameter **instanceKey**. The service logic returns a key string based on **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 obtains the focus. If the returned key does not map to a started UIAbility instance, the system creates a new UIAbility instance and starts it.
128
129   ```ts
130   import AbilityStage from '@ohos.app.ability.AbilityStage';
131
132   export default class MyAbilityStage extends AbilityStage {
133     onAcceptWant(want): string {
134       // In the AbilityStage instance of the callee, a key value corresponding to a UIAbility instance is returned for UIAbility whose launch type is specified.
135       // In this example, SpecifiedAbility of module1 is returned.
136       if (want.abilityName === 'SpecifiedAbility') {
137         // The returned key string is a custom string.
138         return `SpecifiedAbilityInstance_${want.parameters.instanceKey}`;
139       }
140
141       return '';
142     }
143   }
144   ```
145
146   > **NOTE**
147   >
148   > 1. Assume that the application already has a UIAbility instance created, and the launch type of the UIAbility instance is set to **specified**. If [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called again to start the UIAbility instance, and the [onAcceptWant()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback of [AbilityStage](../reference/apis/js-apis-app-ability-abilityStage.md) matches a created UIAbility instance, the original UIAbility 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.
149   > 2. AbilityStage is not automatically generated in the default project of DevEco Studio. For details about how to create an AbilityStage file, see [AbilityStage Component Container](abilitystage.md).
150
151   For 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, and a new UIAbility instance is created when UIAbility is started in AbilityStage. However, when you open an existing document, the same UIAbility instance is started again in AbilityStage.
152
153   The following steps are used as an example.
154
155   1. Open file A. A UIAbility instance, for example, UIAbility instance 1, is started.
156   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 is started, for example, UIAbility instance 2.
157   3. Return to the home screen and open file B. A new UIAbility instance is started, for example, UIAbility instance 3.
158   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 of 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.
159
160