ApplicationContext (System API)
The ApplicationContext module inherits from Context. It provides application-level context capabilities, including APIs for registering and unregistering the lifecycle of application components.
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.
This topic describes only system APIs provided by the module. For details about its public APIs, see ApplicationContext.
Modules to Import
import { common } from '@kit.AbilityKit';
Instructions
Before calling any APIs in ApplicationContext, obtain an ApplicationContext instance through the context instance.
ApplicationContext.preloadUIExtensionAbility12+
preloadUIExtensionAbility(want: Want): Promise<void>
Preloads a UIExtensionAbility instance. This API uses a promise to return the result.
The preloaded UIExtensionAbility instance is sent to the onCreate lifecycle of the UIExtensionAbility and waits to be loaded by the current application.
A UIExtensionAbility instance can be preloaded for multiple times. Each time a preloaded UIExtensionAbility instance is loaded, the next preloaded UIExtensionAbility instance is sent to the onCreate lifecycle of the UIExtensionAbility.
System capability: SystemCapability.Ability.AbilityRuntime.Core
System API: This is a system API.
| Name | Type | Mandatory | Description |
|---|---|---|---|
| want | Want | Yes | Want information of the UIExtensionAbility. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
| ID | Error Message |
|---|---|
| 201 | The application does not have permission to call the interface. |
| 202 | The application is not system-app, can not use system-api. |
| 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 | Cannot start an invisible component. |
| 16200011 | The context does not exist. |
| 16000050 | Internal error. |
Example
import { UIAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate() {
let want: Want = {
bundleName: 'com.ohos.uiextensionprovider',
abilityName: 'UIExtensionProvider',
moduleName: 'entry',
parameters: {
// The value must be the same as the value of type in the module.json5 file of the UIExtensionAbility.
'ability.want.params.uiExtensionType': 'sys/commonUI'
}
};
try {
let applicationContext = this.context.getApplicationContext();
applicationContext.preloadUIExtensionAbility(want)
.then(() => {
// Carry out normal service processing.
console.info('preloadUIExtensionAbility succeed');
})
.catch((err: BusinessError) => {
// Process service logic errors.
console.error('preloadUIExtensionAbility failed');
});
} catch (err) {
// Process input parameter errors.
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`preloadUIExtensionAbility failed. code: ${code}, msg: ${message}`);
}
}
}