UIAbility的启动模式是指UIAbility实例在启动时的不同呈现状态。针对不同的业务场景,系统提供了三种启动模式:
singleton启动模式
singleton启动模式为单实例模式,也是默认情况下的启动模式。
每次调用startAbility()方法时,如果应用进程中该类型的UIAbility实例已经存在,则复用系统中的UIAbility实例。系统中只存在唯一一个该UIAbility实例,即在最近任务列表中只存在一个该类型的UIAbility实例。
说明
应用的UIAbility实例已创建,该UIAbility配置为单实例模式,再次调用startAbility()方法启动该UIAbility实例。由于启动的还是原来的UIAbility实例,并未重新创建一个新的UIAbility实例,此时只会进入该UIAbility的onNewWant()回调,不会进入其onCreate()和onWindowStageCreate()生命周期回调。
如果需要使用singleton启动模式,在module.json5配置文件中的launchType字段配置为singleton即可。
1 2 3 4 5 6 7 8 9 10 11 |
{ "module": { ... "abilities": [ { "launchType": "singleton", ... } ] } } |
multiton启动模式
multiton启动模式为多实例模式,每次调用startAbility()方法时,都会在应用进程中创建一个新的该类型UIAbility实例。
在最近任务列表中可以看到多个该类型的UIAbility实例。
multiton启动模式的开发使用,在module.json5配置文件中的launchType字段配置为multiton即可。
1 2 3 4 5 6 7 8 9 10 11 |
{ "module": { ... "abilities": [ { "launchType": "multiton", ... } ] } } |
specified启动模式
specified启动模式为指定实例模式,针对一些特定场景使用。
例如:文档应用中,每次新建文档都能新建一个文档实例;重复打开一个已保存的文档希望打开的都是同一个文档实例。
在SpecifiedAbility中,将module.json5配置文件的launchType字段配置为specified。
1 2 3 4 5 6 7 8 9 10 11 |
{ "module": { ... "abilities": [ { "launchType": "specified", ... } ] } } |
示例源码
myabilitystage/MyAbilityStage.ets
由于SpecifiedAbility的启动模式被配置为指定实例启动模式,因此在SpecifiedAbility启动之前,会先进入对应的AbilityStage的onAcceptWant()生命周期回调中,以获取该UIAbility实例的Key值。然后系统会自动匹配,如果存在与该UIAbility实例匹配的Key,则会启动与之绑定的UIAbility实例,并进入该UIAbility实例的onNewWant()回调函数;否则会创建一个新的UIAbility实例,并进入该UIAbility实例的onCreate()回调函数和onWindowStageCreate()回调函数。
示例代码中,通过实现onAcceptWant()生命周期回调函数,解析传入的want参数,获取自定义参数instanceKey。业务逻辑会根据这个参数返回一个字符串Key,用于标识当前UIAbility实例。如果返回的Key已经对应一个已启动的UIAbility实例,系统会将该UIAbility实例拉回前台并获焦,而不会创建新的实例。如果返回的Key没有对应已启动的UIAbility实例,则系统会创建新的UIAbility实例并启动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import { AbilityStage, Want } from '@kit.AbilityKit' export default class MyAbilityStage extends AbilityStage { onCreate(): void { // 应用的HAP在首次加载时,为该Module初始化操作 } onAcceptWant(want: Want): string { // 仅specified模式下触发 // 在被调用方的AbilityStage中,针对启动模式为specified的UIAbility返回一个UIAbility实例对应的一个Key值 if (want.abilityName === 'SpecifiedFirstAbility' || want.abilityName === 'SpecifiedSecondAbility') { if (want.parameters) { // 返回的字符串Key标识为自定义拼接的字符串内容 return `SpecifiedAbilityInstance_${want.parameters.instanceKey}` } } return 'MyAbilityStage' } } |
specifiedability/SpecifiedFirstAbility.ets
1 2 3 4 5 6 7 8 9 10 11 |
import { UIAbility } from '@kit.AbilityKit' import { window } from '@kit.ArkUI' export default class SpecifiedFirstAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // 设置UI加载 windowStage.loadContent('pages/SpecifiedFirstIndex', (err, data) => { console.info('loadContent') }) } } |
specifiedability/SpecifiedSecondAbility.ets
specifiedability/SpecifiedSecondAbility.ets
1 2 3 4 5 6 7 8 9 10 11 |
import { UIAbility } from '@kit.AbilityKit' import { window } from '@kit.ArkUI' export default class SpecifiedSecondAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { // 设置UI加载 windowStage.loadContent('pages/SpecifiedSecondIndex', (err, data) => { console.info('loadContent') }) } } |
module.json5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
{ "module": { "name": "entry", "type": "entry", "srcEntry": "./ets/myabilitystage/MyAbilityStage.ets", "description": "$string:module_desc", "mainElement": "EntryAbility", "deviceTypes": [ "phone", "tablet", "2in1" ], "deliveryWithInstall": true, "installationFree": false, "pages": "$profile:main_pages", "abilities": [ { "launchType":"specified", "name": "EntryAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:layered_image", "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true, "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ] }, { "launchType":"specified", "name": "SpecifiedFirstAbility", "srcEntry": "./ets/specifiedability/SpecifiedFirstAbility.ets", "description": "$string:SpecifiedFirstAbility_desc", "icon": "$media:layered_image", "label": "$string:SpecifiedFirstAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true }, { "launchType":"specified", "name": "SpecifiedSecondAbility", "srcEntry": "./ets/specifiedability/SpecifiedSecondAbility.ets", "description": "$string:SpecifiedSecondAbility_desc", "icon": "$media:layered_image", "label": "$string:SpecifiedSecondAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true } ], "extensionAbilities": [ { "name": "EntryBackupAbility", "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", "type": "backup", "exported": false, "metadata": [ { "name": "ohos.extension.backup", "resource": "$profile:backup_config" } ], } ] } } |
pages/Index.ets
在创建UIAbility实例之前,开发者可以为该实例指定一个唯一的字符串Key,这样在调用startAbility()方法时,应用就可以根据指定的Key来识别响应请求的UIAbility实例。在EntryAbility中,调用startAbility()方法时,可以在want参数中增加一个自定义参数,例如instanceKey,以此来区分不同的UIAbility实例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import { common, Want } from '@kit.AbilityKit' import { hilog } from '@kit.PerformanceAnalysisKit' import { BusinessError } from '@kit.BasicServicesKit' const TAG: string = '[Page_StartModel]' const DOMAIN_NUMBER: number = 0xFF00 function getInstance(): string { return 'KEY' } @Entry @Component struct Page_StartModel { private KEY_NEW = 'KEY' build() { Row() { Column() { Text('EntryAbility') Button('新建一个文档') .onClick(() => { let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext // context为调用方UIAbility的UIAbilityContext let want: Want = { deviceId: '', // deviceId为空表示本设备 bundleName: 'com.example.myapplication', abilityName: 'SpecifiedFirstAbility', moduleName: 'entry', // moduleName非必选 parameters: { instanceKey: this.KEY_NEW } } context.startAbility(want).then(() => { hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting SpecifiedAbility.') }).catch((err: BusinessError) => { hilog.error(DOMAIN_NUMBER, TAG, `Failed to start SpecifiedAbility. Code is ${err.code}, message is ${err.message}`) }) this.KEY_NEW = this.KEY_NEW + 'a' }) Button('打开已保存文档') .onClick(() => { let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext // context为调用方UIAbility的UIAbilityContext let want: Want = { deviceId: '', // deviceId为空表示本设备 bundleName: 'com.example.myapplication', abilityName: 'SpecifiedSecondAbility', moduleName: 'entry', // moduleName非必选 parameters: { instanceKey: getInstance() } } context.startAbility(want).then(() => { hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting SpecifiedAbility.') }).catch((err: BusinessError) => { hilog.error(DOMAIN_NUMBER, TAG, `Failed to start SpecifiedAbility. Code is ${err.code}, message is ${err.message}`) }) this.KEY_NEW = this.KEY_NEW + 'a' }) } .width('100%') } .height('100%') } } |
pages/SpecifiedFirstIndex.ets
1 2 3 4 5 6 7 8 9 |
@Entry @Component struct SpecifiedFirstIndex { build() { Column() { Text('新建的文档') } } } |
pages/SpecifiedSecondIndex.ets
1 2 3 4 5 6 7 8 9 |
@Entry @Component struct SpecifiedSecondIndex { build() { Column() { Text('已保存的文档') } } } |