Read the original article:Differences Between the getContext and getHostContext Methods
Question
In the API reference document, the getContext method is discarded since API version 18. You are advised to replace it with the getHostContext method in UIContext. When the return value type is annotated as Context, an error is reported during compilation. What are the differences between the two?
getContext: Obtains the Context object associated with the page context component.
API version 18 and later versions are discarded. You are advised to use getHostContext in UIContext.
Starting with API version 12, the execution context of the UI can be specified by using getHostContext in the UIContext.
getHostContext: Obtains the context of the current meta capability.
Short Answer
getContext: Obtains the Context object associated with the page context component.
API version 18 and later versions are discarded. You are advised to use getHostContext in UIContext.
Starting with API version 12, the execution context of the UI can be specified by using getHostContext in the UIContext.
getHostContext: Obtains the context of the current meta capability.
// The getContext method does not need to cast the result to Context through type assertions.
private context: Context = getContext(this);
// The getHostContext method needs to forcibly convert the result to context through type assertions. Otherwise, an error is reported during compilation.
private context: Context = this.getUIContext().getHostContext() as Context;
Q: As mentioned in this document, getHostContext is the context for obtaining the current meta capability. What is a meta capability?
A: Meta capabilities are abstract functions provided by HarmonyOS applications. A meta-capability provides an application model for developers to develop and run applications. It is an abstraction of the capabilities required by applications provided by the system. It provides necessary components and running mechanisms for applications. With the application model, developers can develop applications based on a unified model, which makes application development simpler and more efficient.
Application scenario:
Multi-module development of applications: Applications can use different types of modules (HAP, HAR, and HSP) to implement application function development. The HAP is used to implement functions and features of an application, and the HAR and the HSP are used to implement code and resource sharing.
Intra-application interaction: Different components in an application can jump to each other. For example, in a payment application, the collection and payment UIAbility component is started through the entry UIAbility component.
Interaction between applications: The current application can start other applications to complete a task or operation. For example, a browser application is launched to open a website, a file application is launched to browse or edit a file, and the like.
Cross-device application transfer: Through cross-device application migration and multi-device collaboration, better user experience is achieved. For example, if a video is played on a tablet, the video is migrated to the Huawei Smart Screen for playback.
Top comments (0)