Intermediate: Ability Framework in HarmonyOS Part-1

Shiddalingeshwar M S
6 min readDec 10, 2021

--

In this article, we will learn about Ability Framework in HarmonyOS and how we can use the Ability Framework in HarmonyOS. I have created sample HarmonyOS application called NoBrokerApp to demonstrate the Ability Framework. Let’s dive into Ability Framework.

Introduction

In this article, we will learn about Ability Framework in HarmonyOS and how we can use the Ability Framework in HarmonyOS. I have created sample HarmonyOS application called NoBrokerApp to demonstrate the Ability Framework. Let’s dive into Ability Framework.

What is an Ability?

An ability is an abstract form of a functionality that an application can provide. The Ability class is a very essential component in HarmonyOS applications. An application may contain various Abilities. These abilities can be deployed together or independently from each other. HarmonyOS offers two types of abilities.

  1. Feature Ability (FA)
  2. Particle Ability (PA)

Each type has their respective templates for different capabilities.

  1. FAs use the Page template.

The Page template is the only template FAs can use. It is used to provide the capability of interacting with users. An ability instance using the Page template can contain a group of related pages, ach page is represented by an AbilitySlice instance.

  1. PAs use the Service and Data templates.
  • The Service template is used for PAs that provide background tasks.
  • The Data template is used for PAs that provide external data access.

An application may contain various Abilities, these must be specified with the template used by an ability and must be registered in the config.json file whenever you declare the ability. You need to set the type attribute of the ability to page, service, or data for using the Page, Service or Data template, respectively.

The following code snippet illustrates, how to set the type attribute in config.json, when we use Page ability as an example to illustrate.

Page Ability and Ability Slice

The Page template is the only template FAs can use. It is used to provide the capability of interacting (UI) with users. An Ability Slice represents a single screen and its control logic.

A Page ability may contain one ability slice or multiple ability slices that provide highly relevant capabilities. For example, a Page ability for NoBrokerApp can contain two ability slices, one for displaying the Houses via Page Slider, and the other for showing House details. Below figure shows the relationship between a Page ability and its ability slices.

Setting Routes

When a Page ability appears in the foreground, it presents one of its ability slices by default. You can set the default ability slice by calling setMainRoute(). If you want to display another ability slice, you can call addActionRoute() to set an action route for showing the ability slice. If another Page ability wants to navigate to this ability slice after such a route is set, you can specify the corresponding action in the Intent.

The following code snippet shows how to set setMainRoute() and addActionRoute().

The action passed to the addActionRoute() method must be declared in the config.json file. The following sample code snippet shows how to declare actions:

Page Ability and Ability Slice Lifecycle

System management actions or user behaviour on your application will cause Page abilities in your application to switch between different states in their lifecycle. The Ability class provides some lifecycle call backs for the Page ability to know that the ability lifecycle state has changed.

Properly handling lifecycle state changes (for example, releasing resources) helps improve the performance robustness of your application.

Below figure shows transitions between different lifecycle states of a Page ability and the callbacks to be invoked during the transitions.

Note: The INACTIVE state is transient. An ability in this state can be regarded as being activated.

Ability Slice Lifecycle

An ability slice’s lifecycle is bound to the Page ability that hosts it. An ability slice has the same lifecycle states and lifecycle callbacks as its host Page ability. Once the lifecycle state of a Page ability changes, its ability slices undergo the same change. An ability slice can also have its own lifecycle state changes, which occur during the switching between the ability slices within the Page ability. Such switching does not affect the host ability’s lifecycle.

The ability slice lifecycle callbacks are similar to Page Ability Lifecycle callbacks. Ability slices are used to present the actual user interface (UI). You must override the onStart() callback of ability slices and use setUIContent() to set the UI content to display in this callback. Here is an example of setting the UI content in onStart():

Most of the cases, AbilitySlice instances are created and managed by the application. The system creates instances only in certain cases, for example, when an AbilitySlice instance is started during application startup, the system creates the instance; when another ability slice within the host Page ability needs to be presented, the application is responsible for creating it.

Switching Between Ability Slices of Different Page Abilities

Framework provides present() method to implement the switching if the source and target ability slices are hosted by the same Page ability. The following code snippet shows how to switch to another ability slice after the user clicks a particular button.

If you expect to receive the return result when the user returns from the target ability slice, you can use the presentForResult() method to implement the switching instead. In that case, you can also override the onResult() callback, so that the system can call it to receive and handle the return result. The result is set by the target ability slice through setResult() within its lifecycle.

Development Overview

You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.
  • A Harmony OS phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK installation package.
  • Latest DevEcho studio installed.

Steps:

Step 1: Create HarmonyOS Application.

Let’s start coding

Result

Tips and Tricks

  • Add required dependencies without fail.
  • Add required images in resources > base > media.
  • Add custom strings in resources > base > element > string.json.
  • Define supporting devices in config.json file.
  • Define actions in config.json.
  • Add properties and values xml files in animation folder.
  • Do not log the sensitive data.
  • Use respective Log methods to print logs.

Conclusion

In this article, we have learnt that how to use Ability Framework classes NoBrokerApp and ability page with multiple ability slices, and switch between ability slices. Hope this article helps you to understand the working of Ability Framework in HarmonyOS. More details you can refer below link.

Thank you so much for reading article and please provide your valuable feedback and like.

Reference

Ability Framework in HarmonyOS

Checkout in forum

--

--

No responses yet