# UIAbilityContext **UIAbilityContext**, inherited from [Context](js-apis-inner-application-context.md), provides the context environment for [UIAbility](js-apis-app-ability-uiAbility.md) that needs to store its status. **UIAbilityContext** provides UIAbility-related configuration and APIs for operating UIAbilities and ServiceExtensionAbilities. For example, you can use the APIs to start a UIAbility, terminate a UIAbility to which the UIAbilityContext belongs, and start, terminate, connect to, or disconnect from a ServiceExtensionAbility. > **NOTE** > > - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. > - The APIs of this module can be used only in the stage model. ## Modules to Import ```ts import { common } from '@kit.AbilityKit'; ``` ## Properties **System capability**: SystemCapability.Ability.AbilityRuntime.Core | Name| Type| Readable| Writable| Description| | -------- | -------- | -------- | -------- | -------- | | abilityInfo | [AbilityInfo](js-apis-bundleManager-abilityInfo.md) | Yes| No| UIAbility information.
**Atomic service API**: This API can be used in atomic services since API version 11.| | currentHapModuleInfo | [HapModuleInfo](js-apis-bundleManager-hapModuleInfo.md) | Yes| No| HAP information.
**Atomic service API**: This API can be used in atomic services since API version 11.| | config | [Configuration](js-apis-app-ability-configuration.md) | Yes| No| UIAbility configuration, such as the language and color mode.
**Atomic service API**: This API can be used in atomic services since API version 11.| | windowStage12+ | [window.WindowStage](../apis-arkui/js-apis-window.md#windowstage9) | Yes| No| **WindowStage** object. It can be called only by the main thread.
**Atomic service API**: This API can be used in atomic services since API version 12.| > **NOTE** > > In the sample code provided in this topic, **this.context** is used to obtain the UIAbilityContext, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../../application-models/uiability-usage.md#obtaining-the-context-of-uiability). ## UIAbilityContext.startAbility startAbility(want: Want, callback: AsyncCallback<void>): void Starts an ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| | callback | AsyncCallback<void> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; try { this.context.startAbility(want, (err: BusinessError) => { if (err.code) { // Process service logic errors. console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('startAbility succeed'); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbility startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void Starts an ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| | options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| | callback | AsyncCallback<void> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 801 | Capability not support. | | 16000001 | The specified ability does not exist. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000067 | The StartOptions check failed. | | 16000068 | The ability is already running. | | 16300003 | The target application is not self application. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0 }; try { this.context.startAbility(want, options, (err: BusinessError) => { if (err.code) { // Process service logic errors. console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('startAbility succeed'); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbility startAbility(want: Want, options?: StartOptions): Promise<void> Starts an ability. This API uses a promise to return the result. It can be called only by the main thread. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| | options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 801 | Capability not support. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000067 | The StartOptions check failed. | | 16000068 | The ability is already running. | | 16300003 | The target application is not self application. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0 }; try { this.context.startAbility(want, options) .then(() => { // Carry out normal service processing. console.info('startAbility succeed'); }) .catch((err: BusinessError) => { // Process service logic errors. console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityForResult startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void Starts an ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. The following situations may be possible for a started ability: - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| | callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want, common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; try { this.context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { if (err.code) { // Process service logic errors. console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('startAbilityForResult succeed'); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityForResult startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void Starts an ability with the start options specified. This API uses an asynchronous callback to return the result. It can be called only by the main thread. The following situations may be possible for a started ability: - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| | options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| | callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want, common, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0 }; try { this.context.startAbilityForResult(want, options, (err: BusinessError, result: common.AbilityResult) => { if (err.code) { // Process service logic errors. console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('startAbilityForResult succeed'); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityForResult startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult> Starts an ability. This API uses a promise to return the result. It can be called only by the main thread. The following situations may be possible for a started ability: - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| | options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| **Return value** | Type| Description| | -------- | -------- | | Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | | 16000019 | No matching ability is found. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | | 16000082 | The UIAbility is being started. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want, common, StartOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0 }; try { this.context.startAbilityForResult(want, options) .then((result: common.AbilityResult) => { // Carry out normal service processing. console.info('startAbilityForResult succeed'); }) .catch((err: BusinessError) => { // Process service logic errors. console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.terminateSelf terminateSelf(callback: AsyncCallback<void>): void Terminates this ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. > **NOTE** > > After this API is called, missions in Recents are not cleared by default. To clear missions, set [removeMissionAfterTerminate](../../quick-start/module-configuration-file.md#abilities) to **true**. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callback | AsyncCallback<void> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { try { this.context.terminateSelf((err: BusinessError) => { if (err.code) { // Process service logic errors. console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('terminateSelf succeed'); }); } catch (err) { // Capture the synchronization parameter error. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`terminateSelf failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.terminateSelf terminateSelf(): Promise<void> Terminates this ability. This API uses a promise to return the result. It can be called only by the main thread. > **NOTE** > > After this API is called, missions in Recents are not cleared by default. To clear missions, set [removeMissionAfterTerminate](../../quick-start/module-configuration-file.md#abilities) to **true**. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise used to return the result.| **Error codes** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { try { this.context.terminateSelf() .then(() => { // Carry out normal service processing. console.info('terminateSelf succeed'); }) .catch((err: BusinessError) => { // Process service logic errors. console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Capture the synchronization parameter error. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`terminateSelf failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.terminateSelfWithResult terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void Terminates this ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. If the ability is started by calling [startAbilityForResult](#uiabilitycontextstartabilityforresult), the result is returned to the caller when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called. > **NOTE** > > After this API is called, missions in Recents are not cleared by default. To clear missions, set [removeMissionAfterTerminate](../../quick-start/module-configuration-file.md#abilities) to **true**. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.| | callback | AsyncCallback<void> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, Want, common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let resultCode = 100; // AbilityResult information returned to the caller. let abilityResult: common.AbilityResult = { want, resultCode }; try { this.context.terminateSelfWithResult(abilityResult, (err: BusinessError) => { if (err.code) { // Process service logic errors. console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('terminateSelfWithResult succeed'); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.terminateSelfWithResult terminateSelfWithResult(parameter: AbilityResult): Promise<void> Terminates this ability. This API uses a promise to return the result. It can be called only by the main thread. If the ability is started by calling [startAbilityForResult](#uiabilitycontextstartabilityforresult), the result is returned to the caller when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called. > **NOTE** > > After this API is called, missions in Recents are not cleared by default. To clear missions, set [removeMissionAfterTerminate](../../quick-start/module-configuration-file.md#abilities) to **true**. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, Want, common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let resultCode = 100; // AbilityResult information returned to the caller. let abilityResult: common.AbilityResult = { want, resultCode }; try { this.context.terminateSelfWithResult(abilityResult) .then(() => { // Carry out normal service processing. console.info('terminateSelfWithResult succeed'); }) .catch((err: BusinessError) => { // Process service logic errors. console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.connectServiceExtensionAbility connectServiceExtensionAbility(want: Want, options: ConnectOptions): number Connects this ability to a ServiceExtensionAbility. This API can be called only by the main thread. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | Yes| Want information for connecting to the ServiceExtensionAbility.| | options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Instance of the callback function after the connection to the ServiceExtensionAbility is set up.| **Return value** | Type| Description| | -------- | -------- | | number | Result code of the connection.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | **Example** ```ts import { UIAbility, Want, common } from '@kit.AbilityKit'; import { rpc } from '@kit.IPCKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'ServiceExtensionAbility' }; let commRemote: rpc.IRemoteObject; let options: common.ConnectOptions = { onConnect(elementName, remote) { commRemote = remote; console.info('onConnect...'); }, onDisconnect(elementName) { console.info('onDisconnect...'); }, onFailed(code) { console.info('onFailed...'); } }; let connection: number; try { connection = this.context.connectServiceExtensionAbility(want, options); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.disconnectServiceExtensionAbility disconnectServiceExtensionAbility(connection: number): Promise\ Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses a promise to return the result. It can be called only by the main thread. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.| **Return value** | Type| Description| | -------- | -------- | | Promise\ | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility } from '@kit.AbilityKit'; import { rpc } from '@kit.IPCKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { // connection is the return value of connectServiceExtensionAbility. let connection = 1; let commRemote: rpc.IRemoteObject | null; try { this.context.disconnectServiceExtensionAbility(connection).then(() => { commRemote = null; // Carry out normal service processing. console.info('disconnectServiceExtensionAbility succeed'); }).catch((err: BusinessError) => { // Process service logic errors. console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { commRemote = null; // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.disconnectServiceExtensionAbility disconnectServiceExtensionAbility(connection: number, callback: AsyncCallback\): void Disconnects this ability from a ServiceExtensionAbility and after the successful disconnection, sets the remote object returned upon the connection to void. This API uses an asynchronous callback to return the result. It can be called only by the main thread. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.| | callback | AsyncCallback\ | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility } from '@kit.AbilityKit'; import { rpc } from '@kit.IPCKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { // connection is the return value of connectServiceExtensionAbility. let connection = 1; let commRemote: rpc.IRemoteObject | null; try { this.context.disconnectServiceExtensionAbility(connection, (err: BusinessError) => { commRemote = null; if (err.code) { // Process service logic errors. console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('disconnectServiceExtensionAbility succeed'); }); } catch (err) { commRemote = null; // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.startAbilityByCall startAbilityByCall(want: Want): Promise<Caller> Starts an ability in the foreground or background in the cross-device scenario and obtains the caller object for communicating with the ability. This API uses a promise to return the result. It can be called only by the main thread. This API cannot be used to start the UIAbility with the launch type set to [specified](../../application-models/uiability-launch-type.md#specified). > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC > **NOTE** > > In versions earlier than API version 11, this API requires the **ohos.permission.ABILITY_BACKGROUND_COMMUNICATION** permission, which is available only for system applications. Since API version 11, this API requires the **ohos.permission.DISTRIBUTED_DATASYNC** permission. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId**, and **parameters** (optional). If **parameters** is left blank or null, the ability is started in the background.| **Return value** | Type| Description| | -------- | -------- | | Promise<[Caller](js-apis-app-ability-uiAbility.md#caller)> | Promise used to return the caller object to communicate with.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | | 16000050 | Internal error. | | 16000071 | App clone is not supported. | | 16000072 | App clone or multi-instance is not supported. | | 16000073 | The app clone index is invalid. | | 16000076 | The app instance key is invalid. | | 16000077 | The number of app instances reaches the limit. | | 16000078 | The multi-instance is not supported. | | 16000079 | The APP_INSTANCE_KEY cannot be specified. | | 16000080 | Creating an instance is not supported. | **Example** Start an ability in the background. ```ts import { UIAbility, Caller, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let caller: Caller; // Start an ability in the background by not passing parameters. let wantBackground: Want = { bundleName: 'com.example.myapplication', moduleName: 'entry', abilityName: 'EntryAbility', deviceId: '' }; try { this.context.startAbilityByCall(wantBackground) .then((obj: Caller) => { // Carry out normal service processing. caller = obj; console.info('startAbilityByCall succeed'); }).catch((err: BusinessError) => { // Process service logic errors. console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityByCall failed, code is ${code}, message is ${message}`); } } } ``` Start an ability in the foreground. ```ts import { UIAbility, Caller, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let caller: Caller; // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true. let wantForeground: Want = { bundleName: 'com.example.myapplication', moduleName: 'entry', abilityName: 'EntryAbility', deviceId: '', parameters: { 'ohos.aafwk.param.callAbilityToForeground': true } }; try { this.context.startAbilityByCall(wantForeground) .then((obj: Caller) => { // Carry out normal service processing. caller = obj; console.info('startAbilityByCall succeed'); }).catch((err: BusinessError) => { // Process service logic errors. console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbilityByCall failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.setMissionLabel setMissionLabel(label: string, callback: AsyncCallback<void>): void Sets a label for this ability in the mission. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | label | string | Yes| Label of the ability to set.| | callback | AsyncCallback<void> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { this.context.setMissionLabel('test', (result: BusinessError) => { console.info(`setMissionLabel: ${JSON.stringify(result)}`); }); } } ``` ## UIAbilityContext.setMissionLabel setMissionLabel(label: string): Promise<void> Sets a label for this ability in the mission. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | label | string | Yes| Label of the ability to set.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { this.context.setMissionLabel('test').then(() => { console.info('success'); }).catch((err: BusinessError) => { let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`setMissionLabel failed, code is ${code}, message is ${message}`); }); } } ``` ## UIAbilityContext.setMissionContinueState10+ setMissionContinueState(state: AbilityConstant.ContinueState, callback: AsyncCallback<void>): void Sets the mission continuation state of this ability. This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#continuestate10) | Yes| Mission continuation state.| | callback | AsyncCallback<void> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => { console.info(`setMissionContinueState: ${JSON.stringify(result)}`); }); } } ``` ## UIAbilityContext.setMissionContinueState10+ setMissionContinueState(state: AbilityConstant.ContinueState): Promise<void> Sets the mission continuation state of this ability. This API uses a promise to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#continuestate10) | Yes| Mission continuation state.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE).then(() => { console.info('success'); }).catch((err: BusinessError) => { console.error(`setMissionContinueState failed, code is ${err.code}, message is ${err.message}`); }); } } ``` ## UIAbilityContext.restoreWindowStage restoreWindowStage(localStorage: LocalStorage): void Restores the WindowStage data in the ability. It can be called only by the main thread. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | localStorage | LocalStorage | Yes| Storage used to store the restored window stage.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility } from '@kit.AbilityKit'; export default class EntryAbility extends UIAbility { onForeground() { let storage = new LocalStorage(); this.context.restoreWindowStage(storage); } } ``` ## UIAbilityContext.isTerminating isTerminating(): boolean Checks whether this ability is in the terminating state. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | boolean | The value **true** means that the ability is in the terminating state, and **false** means the opposite.| **Error codes** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 16000011 | The context does not exist. | **Example** ```ts import { UIAbility } from '@kit.AbilityKit'; export default class EntryAbility extends UIAbility { onForeground() { let isTerminating: boolean = this.context.isTerminating(); console.info(`ability state is ${isTerminating}`); } } ``` ## UIAbilityContext.requestDialogService requestDialogService(want: Want, result: AsyncCallback<dialogRequest.RequestResult>): void Starts a ServiceExtensionAbility that supports modal dialog boxes. After the ServiceExtensionAbility is started, the application displays a modal dialog box. You can call [setRequestResult](js-apis-app-ability-dialogRequest.md#requestcallbacksetrequestresult) to obtain the result. This API uses an asynchronous callback to return the result. It can be called only by the main thread. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want |[Want](js-apis-app-ability-want.md) | Yes| Want information for starting the ServiceExtensionAbility.| | result | AsyncCallback<[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md#requestresult)> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want, dialogRequest } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'AuthAccountServiceExtension' }; try { this.context.requestDialogService(want, (err: BusinessError, result: dialogRequest.RequestResult) => { if (err.code) { // Process service logic errors. console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info(`requestDialogService succeed, result = ${JSON.stringify(result)}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`requestDialogService failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.requestDialogService requestDialogService(want: Want): Promise<dialogRequest.RequestResult> Starts a ServiceExtensionAbility that supports modal dialog boxes. After the ServiceExtensionAbility is started, the application displays a modal dialog box. You can call [setRequestResult](js-apis-app-ability-dialogRequest.md#requestcallbacksetrequestresult) to obtain the result. This API uses a promise to return the result. It can be called only by the main thread. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | want | [Want](js-apis-app-ability-want.md) | Yes| Want information for starting the ServiceExtensionAbility.| **Return value** | Type| Description| | -------- | -------- | | Promise<[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md)> | Promise used to return the result. **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, Want, dialogRequest } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { bundleName: 'com.example.myapplication', abilityName: 'AuthAccountServiceExtension' }; try { this.context.requestDialogService(want) .then((result: dialogRequest.RequestResult) => { // Carry out normal service processing. console.info(`requestDialogService succeed, result = ${JSON.stringify(result)}`); }) .catch((err: BusinessError) => { // Process service logic errors. console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`requestDialogService failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.reportDrawnCompleted10+ reportDrawnCompleted(callback: AsyncCallback\): void Reports an event indicating that page loading is complete (**loadContent()** is successfully called). This API uses an asynchronous callback to return the result. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callback | AsyncCallback<void> | Yes| Callback used to report that page loading is complete.| **Error codes** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { return; } try { this.context.reportDrawnCompleted((err) => { if (err.code) { // Process service logic errors. console.error(`reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('reportDrawnCompleted succeed'); }); } catch (err) { // Capture the synchronization parameter error. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`reportDrawnCompleted failed, code is ${code}, message is ${message}`); } }); console.log("MainAbility onWindowStageCreate"); } }; ``` ## UIAbilityContext.startAbilityByType11+ startAbilityByType(type: string, wantParam: Record, abilityStartCallback: AbilityStartCallback, callback: AsyncCallback\) : void Implicitly starts a given type of UIExtensionAbility. This API uses an asynchronous callback to return the result. It can be called only in the main thread and by applications running in the foreground. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | type | string | Yes| Type of the UIExtensionAbility to start. For details, see [Starting an Application of the Specified Type](../../application-models/start-intent-panel.md#matching-rules).| | wantParam | Record<string, Object> | Yes| Extended parameter.| | abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | Yes| Callback used to return the result.| | callback | AsyncCallback<void> | Yes| Callback used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, common } from '@kit.AbilityKit'; export default class EntryAbility extends UIAbility { onForeground() { let wantParam: Record = { 'time': '2023-10-23 20:45' }; let abilityStartCallback: common.AbilityStartCallback = { onError: (code: number, name: string, message: string) => { console.log(`code:` + code + `name:` + name + `message:` + message); }, onResult: (abilityResult: common.AbilityResult) => { console.log(`resultCode:` + abilityResult.resultCode + `bundleName:` + abilityResult.want?.bundleName); } }; this.context.startAbilityByType("photoEditor", wantParam, abilityStartCallback, (err) => { if (err) { console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); } else { console.log(`success`); } }); } } ``` ## UIAbilityContext.startAbilityByType11+ startAbilityByType(type: string, wantParam: Record, abilityStartCallback: AbilityStartCallback) : Promise\ Implicitly starts a given type of UIExtensionAbility. This API uses a promise to return the result. It can be called only in the main thread and by applications running in the foreground. **Atomic service API**: This API can be used in atomic services since API version 11. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | type | string | Yes| Type of the UIExtensionAbility to start. For details, see [Starting an Application of the Specified Type](../../application-models/start-intent-panel.md#matching-rules).| | wantParam | Record<string, Object> | Yes| Extended parameter.| | abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | Yes| Callback used to return the result.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000050 | Internal error. | **Example** ```ts import { UIAbility, common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let wantParam: Record = { 'time': '2023-10-23 20:45' }; let abilityStartCallback: common.AbilityStartCallback = { onError: (code: number, name: string, message: string) => { console.log(`code:` + code + `name:` + name + `message:` + message); }, onResult: (abilityResult: common.AbilityResult) => { console.log(`resultCode:` + abilityResult.resultCode + `bundleName:` + abilityResult.want?.bundleName); } }; this.context.startAbilityByType("photoEditor", wantParam, abilityStartCallback).then(() => { console.log(`startAbilityByType success`); }).catch((err: BusinessError) => { console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); }); } } ``` ## UIAbilityContext.showAbility12+ showAbility(): Promise\ Shows the current ability. This API uses a promise to return the result. It takes effect only on 2-in-1 devices and tablets. It can be called only by the main thread. Before calling this API, ensure that the application has been added to the status bar. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 801 | Capability not support. | | 16000050 | Internal error. | | 16000067 | The StartOptions check failed. | **Example** ```ts // Index.ets import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct Index { @State showAbility: string = 'showAbility' build() { Row() { Column() { Text(this.showAbility) .fontSize(30) .fontWeight(FontWeight.Bold) .onClick(() => { let context = getContext(this) as common.UIAbilityContext; context.showAbility().then(() => { console.log(`showAbility success`); }).catch((err: BusinessError) => { console.error(`showAbility fail, err: ${JSON.stringify(err)}`); }); }); } .width('100%') } .height('100%') } } ``` ```ts // EntryAbility.ts import { UIAbility, Want, StartOptions, contextConstant } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0, processMode: contextConstant.ProcessMode.NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM }; try { this.context.startAbility(want, options, (err: BusinessError) => { if (err.code) { // Process service logic errors. console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('startAbility succeed'); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.hideAbility12+ hideAbility(): Promise\ Hides the current ability. This API uses a promise to return the result. It takes effect only on 2-in-1 devices and tablets. It can be called only by the main thread. Before calling this API, ensure that the application has been added to the status bar. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 801 | Capability not support. | | 16000050 | Internal error. | | 16000067 | The StartOptions check failed. | **Example** ```ts // Index.ets import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct Index { @State hideAbility: string = 'hideAbility' build() { Row() { Column() { Text(this.hideAbility) .fontSize(30) .fontWeight(FontWeight.Bold) .onClick(() => { let context = getContext(this) as common.UIAbilityContext; context.hideAbility().then(() => { console.log(`hideAbility success`); }).catch((err: BusinessError) => { console.error(`hideAbility fail, err: ${JSON.stringify(err)}`); }); }); } .width('100%') } .height('100%') } } ``` ```ts // EntryAbility.ts import { UIAbility, Want, StartOptions, contextConstant } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let want: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'EntryAbility' }; let options: StartOptions = { displayId: 0, processMode: contextConstant.ProcessMode.NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM }; try { this.context.startAbility(want, options, (err: BusinessError) => { if (err.code) { // Process service logic errors. console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); return; } // Carry out normal service processing. console.info('startAbility succeed'); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`startAbility failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.moveAbilityToBackground12+ moveAbilityToBackground(): Promise\ Moves this ability from the foreground to the background. This API uses a promise to return the result. It can be called only by the main thread.
**Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 16000011 | The context does not exist. | | 16000050 | Internal error. | | 16000061 | Operation not supported. | | 16000065 | The API can be called only when the ability is running in the foreground. | | 16000066 | An ability cannot switch to the foreground or background in Wukong mode. | **Example** ```ts import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct Index { @State moveAbilityToBackground: string = 'Move To Background' build() { Row() { Column() { Text(this.moveAbilityToBackground) .fontSize(30) .fontWeight(FontWeight.Bold) .onClick(() => { let context = getContext(this) as common.UIAbilityContext; context.moveAbilityToBackground().then(() => { console.log(`moveAbilityToBackground success.`); }).catch((err: BusinessError) => { console.log(`moveAbilityToBackground error: ${JSON.stringify(err)}.`); }); }); } .width('100%') } .height('100%') } } ``` ## UIAbilityContext.openAtomicService12+ openAtomicService(appId: string, options?: AtomicServiceOptions): Promise<AbilityResult> Starts an [EmbeddableUIAbility](js-apis-app-ability-embeddableUIAbility.md) in jump-out mode and obtains the result. This API uses a promise to return the result. It can be called only by the main thread. The following situations may be possible for a started EmbeddableUIAbility: - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the EmbeddableUIAbility. The result is returned to the caller. - If an exception occurs, for example, the EmbeddableUIAbility is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. - If different applications call this API to start an EmbeddableUIAbility and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the EmbeddableUIAbility, the normal result is returned to the last caller, and an exception message, in which **resultCode** is **-1**, is returned to others. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | appId | string | Yes| Unique ID of the application, which is allocated by the cloud.| | options | [AtomicServiceOptions](js-apis-app-ability-atomicServiceOptions.md) | No| Parameter carried in the request for starting the atomic service in jump-out mode.| **Return value** | Type| Description| | -------- | -------- | | Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result, which is an [AbilityResult](js-apis-inner-ability-abilityResult.md) object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000002 | Incorrect ability type. | | 16000003 | The specified ID does not exist. | | 16000004 | Failed to start the invisible ability. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the UI. | | 16000055 | Installation-free timed out. | | 16200001 | The caller has been released. | **Example** ```ts import { UIAbility, common, AtomicServiceOptions } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onForeground() { let appId: string = '6918661953712445909'; let options: AtomicServiceOptions = { displayId: 0 }; try { this.context.openAtomicService(appId, options) .then((result: common.AbilityResult) => { // Carry out normal service processing. console.info('openAtomicService succeed'); }) .catch((err: BusinessError) => { // Process service logic errors. console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.error(`openAtomicService failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.openLink12+ openLink(link: string, options?: OpenLinkOptions, callback?: AsyncCallback<AbilityResult>): Promise<void> Starts a UIAbility through App Linking. This API uses a promise to return the result. It can be called only by the main thread. A URL in the standard format is passed in to the **link** field to start the target UIAbility based on the implicit Want matching rules. The target UIAbility must have the following filter characteristics to process links of App Linking: - The **actions** field contains **ohos.want.action.viewData**. - The **entities** field contains **entity.system.browsable**. - The **uris** field contains elements whose **scheme** is **https** and **domainVerify** is **true**. If you want to obtain the result after the started UIAbility is terminated, set the **callback** parameter. For details about how to use this parameter, see [startAbilityForResult](#uiabilitycontextstartabilityforresult). If an input parameter is invalid, for example, a mandatory parameter is not set or the URL set in **link** is not in the standard format, an exception is thrown. If the parameter verification is successful but an error occurs when starting the target UIAbility, the error information is returned through promise. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | link | string | Yes| URL to open, which must be in the standard format.| | options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | No| Options of the URL.| | callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | No| Callback used to return the result.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000010 | The call with the continuation flag is forbidden. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000019 | No matching ability is found. | | 16200001 | The caller has been released. | | 16000053 | The ability is not on the top of the UI. | | 16000082 | The UIAbility is being started. | **Example** ```ts import { common, OpenLinkOptions } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; const DOMAIN = 0xeeee; const TAG: string = '[openLinkDemo]'; @Entry @Component struct Index { build() { RelativeContainer() { Button("Call StartAbilityForResult") .onClick(() => { let context = getContext(this) as common.UIAbilityContext; let link: string = 'https://www.example.com'; let openLinkOptions: OpenLinkOptions = { appLinkingOnly: true, parameters: { demo_key: 'demo_value' } }; try { context.openLink( link, openLinkOptions, (err, result) => { hilog.error(DOMAIN, TAG, `openLink callback error.code: ${JSON.stringify(err)}`); hilog.info(DOMAIN, TAG, `openLink callback result: ${JSON.stringify(result.resultCode)}`); hilog.info(DOMAIN, TAG, `openLink callback result data: ${JSON.stringify(result.want)}`); } ).then(() => { hilog.info(DOMAIN, TAG, `open link success.`); }).catch((err: BusinessError) => { hilog.error(DOMAIN, TAG, `open link failed, errCode ${JSON.stringify(err.code)}`); }); } catch (e) { hilog.error(DOMAIN, TAG, `exception occured, errCode ${JSON.stringify(e.code)}`); } }) } .height('100%') .width('100%') } } ``` ## UIAbilityContext.backToCallerAbilityWithResult12+ backToCallerAbilityWithResult(abilityResult: AbilityResult, requestCode: string): Promise<void> Returns the startup result to the caller of [startAbilityForResult](#uiabilitycontextstartabilityforresult) or [openLink](#uiabilitycontextopenlink12). Different from [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult), this API does not destroy the current ability (target ability) when it returns the result. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | abilityResult | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Result returned to the caller.| | requestCode | string | Yes| Request code generated by the system when the target ability is started using [startAbilityForResult](#uiabilitycontextstartabilityforresult) or [openLink](#uiabilitycontextopenlink12). The value can be obtained from the [CALLER_REQUEST_CODE](js-apis-app-ability-wantConstant.md) field in **want**.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | | 16000009 | An ability cannot be started or stopped in Wukong mode. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | | 16000074 | The caller does not exist. | | 16000075 | Not support back to caller. | **Example** The caller uses **startAbilityForResult** to start an ability, and the target ability calls **backToCallerAbilityWithResult** to return the result to the caller. ```ts // Caller // index.ets import { common, Want } from '@kit.AbilityKit'; import { BusinessError } from '@ohos.base'; import { hilog } from '@kit.PerformanceAnalysisKit'; @Entry @Component struct Index { @State message: string = 'Hello World'; build() { Row() { Column() { Text(this.message) .fontSize(30) .fontWeight(FontWeight.Bold) Button("Call StartAbilityForResult") .onClick(() => { let context: common.UIAbilityContext = getContext() as common.UIAbilityContext; let want: Want = { bundleName: 'com.example.demo2', abilityName: 'EntryAbility' }; try { // Use startAbilityForResult to start the target ability. context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { if (err.code) { // Process service logic errors. hilog.error(0x0000, 'testTag', `startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); this.message = `startAbilityForResult failed: code is ${err.code}, message is ${err.message}` return; } // Carry out normal service processing. hilog.info(0x0000, 'testTag', `startAbilityForResult succeed`); hilog.info(0x0000, 'testTag', `AbilityResult is ${JSON.stringify(result)}`); this.message = `AbilityResult.resultCode: ${JSON.stringify(result.resultCode)}` }); } catch (err) { // Process input parameter errors. let code = (err as BusinessError).code; let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', `startAbilityForResult failed, code is ${code}, message is ${message}`); this.message = `startAbilityForResult failed, code is ${code}, message is ${message}`; } }) } .width('100%') } .height('100%') } } ``` ```ts // Target ability // EntryAbility.ets import { AbilityConstant, common, UIAbility, Want, wantConstant } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { // Obtain the CALLER_REQUEST_CODE of the caller from want and save it. let callerRequestCode: string = want?.parameters?.[wantConstant.Params.CALLER_REQUEST_CODE] as string; AppStorage.setOrCreate("callerRequestCode", callerRequestCode) } onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { let callerRequestCode: string = want?.parameters?.[wantConstant.Params.CALLER_REQUEST_CODE] as string; AppStorage.setOrCreate("callerRequestCode", callerRequestCode) } onForeground(): void { // Obtain the saved CALLER_REQUEST_CODE. let callerRequestCode: string = AppStorage.get("callerRequestCode") as string; hilog.info(0x0000, 'testTag', `callerRequestCode is ${callerRequestCode}`); let want: Want = {}; let resultCode = 100; let abilityResult: common.AbilityResult = { want, resultCode }; try { // Return the result to the caller. this.context.backToCallerAbilityWithResult(abilityResult, callerRequestCode) .then(() => { // Carry out normal service processing. hilog.info(0x0000, 'testTag', 'backToCallerAbilityWithResult succeed'); }) .catch((err: BusinessError) => { // Process service logic errors. hilog.error(0x0000, 'testTag', `backToCallerAbilityWithResult failed, code is ${err.code}, message is ${err.message}`); }); } catch (err) { // Capture the synchronization parameter error. let code = (err as BusinessError).code; let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', `backToCallerAbilityWithResult failed, code is ${code}, message is ${message}`); } } } ``` ## UIAbilityContext.setRestoreEnabled14+ setRestoreEnabled(enabled: boolean): Promise\ Sets whether to enable backup and restore for this UIAbility. This API uses a promise to return the result. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | enabled | boolean | Yes| Whether to enable backup and restore. The value **true** means to enable backup and restore, and **false** means the opposite.| **Return value** | Type| Description| | -------- | -------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message| | ------- | -------------------------------- | | 401 | If the input parameter is not valid parameter. | | 16000011 | The context does not exist. | **Example** ```ts import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { let enabled = true; try { this.context.setRestoreEnabled(enabled); } catch (paramError) { let code = (paramError as BusinessError).code; let message = (paramError as BusinessError).message; console.error(`setRestoreEnabled failed, err code: ${code}, err msg: ${message}`); } } } ``` ## UIAbilityContext.startUIServiceExtensionAbility13+ startUIServiceExtensionAbility(want: Want): Promise<void> Starts a UIServiceExtensionAbility. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). **Atomic service API**: This API can be used in atomic services since API version 13. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name | Type | Read Only| Optional| Description | | -------- | --------------------------------------- | ---- | ---- | ------------------------ | | want | [Want](js-apis-app-ability-want.md) | Yes | No| Want information required for startup.| **Return value** | Type | Description | | ------------------- | -------------------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message | | -------- | ----------------------------------------------------------------------------------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 801 | The Ability is not supported. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000012 | The application is controlled. | | 16000013 | The application is controlled by EDM. | | 16000050 | Internal error. | | 16200001 | The caller has been released. | **Example** ```ts import { common, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct Index { build() { Column() { Row() { // Create a Start button. Button('start ability') .enabled(true) .onClick(() => { let context = getContext(this) as common.UIAbilityContext; let startWant: Want = { bundleName: 'com.acts.uiserviceextensionability', abilityName: 'UiServiceExtAbility', }; try { // Start the UIServiceExtensionAbility. context.startUIServiceExtensionAbility(startWant).then(() => { console.log('startUIServiceExtensionAbility success'); }).catch((error: BusinessError) => { console.log('startUIServiceExtensionAbility error', JSON.stringify(error)); }) } catch (err) { console.log('startUIServiceExtensionAbility failed', JSON.stringify(err)); } }) } } } } ``` ## UIAbilityContext.connectUIServiceExtensionAbility13+ connectUIServiceExtensionAbility(want: Want, callback: UIServiceExtensionConnectCallback) : Promise<UIServiceProxy> Connects to a UIServiceExtensionAbility. This API uses a promise to return the result. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). > **Atomic service API**: This API can be used in atomic services since API version 13. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Read Only| Optional| Description | | ------ | ---- | ---- | -------------------- | ---- | | want |[Want](js-apis-app-ability-want.md) | Yes | No| Want information required for connection.| | callback | [UIServiceExtensionConnectCallback](js-apis-inner-application-uiServiceExtensionconnectcallback.md) | No| Yes| Callback for connecting to the UIServiceExtensionAbility.| **Return value** | Type | Description | | ------------------- | -------------------------------------- | | Promise<UIServiceProxy> | Promise used to return a [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) object.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message | | -------- | ----------------------------------------------------------------------------------- | | 201 | The application does not have permission to call the interface. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000001 | The specified ability does not exist. | | 16000002 | Incorrect ability type. | | 16000004 | Failed to start the invisible ability. | | 16000005 | The specified process does not have the permission. | | 16000006 | Cross-user operations are not allowed. | | 16000008 | The crowdtesting application expires. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | | 16000053 | The ability is not on the top of the | | 16000055 | Installation-free timed out. | **Example** ```ts import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; const TAG: string = '[Extension] '; @Entry @Component struct UIServiceExtensionAbility { dataCallBack : common.UIServiceExtensionConnectCallback = { // Receive data onData: (data: Record) => { console.log(`dataCallBack received data`, JSON.stringify(data)); }, // Disconnect from the UIServiceExtensionAbility. onDisconnect: () => { console.log(`dataCallBack onDisconnect`); } } async myConnect() { // Obtain the context. let context = getContext(this) as common.UIAbilityContext; let startWant: Want = { deviceId: '', bundleName: 'com.example.myapplication', abilityName: 'UiServiceExtAbility' }; try { // Connect to the UIServiceExtensionAbility. context.connectUIServiceExtensionAbility(startWant, this.dataCallBack) .then((proxy: common.UIServiceProxy) => { console.log(TAG + `try to connectUIServiceExtensionAbility`, JSON.stringify(proxy)); }).catch((err: Error) => { let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); }); } catch (err) { let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); }; } build() { RelativeContainer() { // Create a Connect button. Button('connectServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true }) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { this.myConnect() }); } .height('100%') .width('100%') } } ``` ## UIAbilityContext.disconnectUIServiceExtensionAbility13+ disconnectUIServiceExtensionAbility(proxy: UIServiceProxy): Promise<void> Disconnects from a UIServiceExtensionAbility. > **NOTE** > > For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). > **Atomic service API**: This API can be used in atomic services since API version 13. **System capability**: SystemCapability.Ability.AbilityRuntime.Core **Parameters** | Name| Type| Read Only| Optional| Description | | ------ | ---- | ---- | -------------------- | -------------------- | | proxy | [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) | Yes |No| Proxy returned after [connectUIServiceExtensionAbility](#uiabilitycontextconnectuiserviceextensionability13) is called.| **Return value** | Type | Description | | ------------------- | -------------------------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). | ID| Error Message | | -------- | ------------------------------------------------------------------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 16000011 | The context does not exist. | | 16000050 | Internal error. | **Example** ```ts import { common } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; const TAG: string = '[Extension] '; @Entry @Component struct UIServiceExtensionAbility { comProxy: common.UIServiceProxy | null = null; build() { Scroll() { Column() { // Create a Disconnect button. Button('disconnectUIServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true }) .margin({ top: 5, left: 10, right: 10, bottom: 5 }) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { this.myDisconnectUIServiceExtensionAbility() }); } .width('100%') } .height('100%') } myDisconnectUIServiceExtensionAbility() { let context = getContext(this) as common.UIAbilityContext; try { // Disconnect from the UIServiceExtensionAbility. context.disconnectUIServiceExtensionAbility(this.comProxy) .then(() => { console.log(TAG + `disconnectUIServiceExtensionAbility succeed ${this.comProxy}}`); }).catch((err: Error) => { let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.log(TAG + `disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); }); } catch (err) { let code = (err as BusinessError).code; let message = (err as BusinessError).message; console.log(TAG + `disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); } } } ```