Ability Framework in HarmonyOS Part-2

Shiddalingeshwar M S
3 min readOct 22, 2021

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.

Introduction

In this article, we will learn about Ability Framework in HarmonyOS and how we can use the Ability Framework in NoBrokerApp in HarmonyOS.

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, each 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:

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.
  • 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 Part-1

Ability Framework in HarmonyOS

Checkout in forum

--

--