1# UIAbility Launch Type 2 3 4The launch type of the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) 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-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called, if a [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) 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 23 24> **NOTE** 25> 26> If [startAbility()](../reference/apis-ability-kit/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-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonnewwant) callback is invoked, but the [onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate) and [onWindowStageCreate()](../reference/apis-ability-kit/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-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called, a new [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) 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 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 79 80In the following example, there are two [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) 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-ability-kit/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](../reference/apis-ability-kit/js-apis-app-ability-want.md) parameter in [startAbility()](../reference/apis-ability-kit/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, Want } from '@kit.AbilityKit'; 104 import { hilog } from '@kit.PerformanceAnalysisKit'; 105 import { BusinessError } from '@kit.BasicServicesKit'; 106 107 const TAG: string = '[Page_StartModel]'; 108 const DOMAIN_NUMBER: number = 0xFF00; 109 110 function getInstance(): string { 111 return 'KEY'; 112 } 113 114 @Entry 115 @Component 116 struct Page_StartModel { 117 private KEY_NEW = 'KEY'; 118 119 build() { 120 Row() { 121 Column() { 122 // ... 123 Button()// ... 124 .onClick(() => { 125 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 126 // context is the UIAbilityContext of the initiator UIAbility. 127 let want: Want = { 128 deviceId: '', // An empty deviceId indicates the local device. 129 bundleName: 'com.samples.stagemodelabilitydevelop', 130 abilityName: 'SpecifiedFirstAbility', 131 moduleName: 'entry', // moduleName is optional. 132 parameters: { 133 // Custom information. 134 instanceKey: this.KEY_NEW 135 } 136 }; 137 context.startAbility(want).then(() => { 138 hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting SpecifiedAbility.'); 139 }).catch((err: BusinessError) => { 140 hilog.error(DOMAIN_NUMBER, TAG, `Failed to start SpecifiedAbility. Code is ${err.code}, message is ${err.message}`); 141 }) 142 this.KEY_NEW = this.KEY_NEW + 'a'; 143 }) 144 // ... 145 Button()// ... 146 .onClick(() => { 147 let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 148 // context is the UIAbilityContext of the initiator UIAbility. 149 let want: Want = { 150 deviceId: '', // An empty deviceId indicates the local device. 151 bundleName: 'com.samples.stagemodelabilitydevelop', 152 abilityName: 'SpecifiedSecondAbility', 153 moduleName: 'entry', // moduleName is optional. 154 parameters: { 155 // Custom information. 156 instanceKey: getInstance() 157 } 158 }; 159 context.startAbility(want).then(() => { 160 hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting SpecifiedAbility.'); 161 }).catch((err: BusinessError) => { 162 hilog.error(DOMAIN_NUMBER, TAG, `Failed to start SpecifiedAbility. Code is ${err.code}, message is ${err.message}`); 163 }) 164 this.KEY_NEW = this.KEY_NEW + 'a'; 165 }) 166 // ... 167 } 168 .width('100%') 169 } 170 .height('100%') 171 } 172 } 173 ``` 174 1753. Before SpecifiedAbility is started, the [onAcceptWant()](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback of the corresponding AbilityStage instance is invoked to obtain the key of the target [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md). If a UIAbility instance matching the key exists, the system starts the UIAbility instance and invokes its [onNewWant()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonnewwant) callback. Otherwise, the system creates a new UIAbility instance and invokes its [onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate) and [onWindowStageCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) callbacks. 176 177 In the sample code, the [onAcceptWant()](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback uses the passed [want](../reference/apis-ability-kit/js-apis-app-ability-want.md) 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. 178 179 ```ts 180 import { AbilityStage, Want } from '@kit.AbilityKit'; 181 182 export default class MyAbilityStage extends AbilityStage { 183 onAcceptWant(want: Want): string { 184 // In the AbilityStage instance of the callee, a key string corresponding to a UIAbility instance is returned for UIAbility whose launch type is specified. 185 // In this example, SpecifiedAbility of module1 is returned. 186 if (want.abilityName === 'SpecifiedFirstAbility' || want.abilityName === 'SpecifiedSecondAbility') { 187 // The returned key string is a custom string. 188 if (want.parameters) { 189 return `SpecifiedAbilityInstance_${want.parameters.instanceKey}`; 190 } 191 } 192 // ... 193 return 'MyAbilityStage'; 194 } 195 } 196 ``` 197 198 > **NOTE** 199 > 200 > - If [startAbility()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is called to start an existing UIAbility instance in specified mode, and the [onAcceptWant()](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md#abilitystageonacceptwant) callback of [AbilityStage](../reference/apis-ability-kit/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()** callback is invoked, but the **onCreate()** and **onWindowStageCreate()** callbacks are not. 201 > 202 > - 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). 203 204For 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](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) instance is created when the UIAbility is started in [AbilityStage](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md). However, when an existing document is opened, the same UIAbility instance is started again in AbilityStage. 205 206The following steps are used as an example. 207 208 1. Open file A. A UIAbility instance, UIAbility instance 1, is started. 209 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. 210 3. Return to the home screen and open file B. A new UIAbility instance, UIAbility instance 3, is started. 211 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. 212