In-depth use of AbilitySlice of Hongmeng Intermediate (basic knowledge of HarmonyOS Hongmeng development)

AbilitySlice

As the basic unit of Ability using Page templates, AbilitySlice provides functions with carriers for service logic and UI display.
A capability can have multiple capability slices. You must override the Ability#onStart(Intent) method to specify the default ability slice. Specify this default route by using Ability#setMainRoute(String).

You can inherit the AbilitySlice class to implement your own ability slice, and set its UI in the onStart (ohos.aafwk.content.Intent) method. You can use setUIContent (ohos.agp.components.ComponentContainer) or setUIContent (int) to set the UI. Generally, in the entire life cycle of a functional slice, you only need to set the UI once. Sample code:

 public class MyAbilitySlice extends AbilitySlice {
     protected void onStart(Intent intent) {
         super.onStart(intent);
         // setup UI content from a layout resource
         setUIContent(R.res.layout);
     }
 }

The preceding code only defines the entry route at runtime. To expose the capabilities of the capability to other capabilities, the operation field must be registered in the configuration file config.json. Sample code:

 {
     "module":{
         ...
         "abilities":[
           {
               ...
               "description": "Main ability of hiworld",
               "name": ".MainAbility",
               "label": "main ability",
               "icon": "main-ability.png",
               "type": "page",
               "visible": true,
               "orientation": "unspecified",
               "launch-mode": "standard",
               "skills"[
                   {
                       "actions":[
                           "action.pay",
                           "action.scan"
                       ]
                   }
               ]
               ...
           }
         ]
         ...
     }
 }
 

After registering the supported actions in the above way, other abilities can use your abilities through the specified actions and directly open the corresponding ability slices. Sample code:

     
     Intent intent = new Intent();
     Operation operation = new Intent.OperationBuilder()
             .withAction("action.pay")
             .withDeviceId("")
             .withBundleName("xxx")
             .withAbilityName("yyy")
             .build();
     intent.setOperation(operation);
     startAbility(intent);
     

AbilitySlice life cycle

The life cycle of a capability slice is related to the capability to carry it. The function slice has the same life cycle state and life cycle callback method as the host function. Once the life cycle state of an ability is changed, its ability slice will undergo the same change, and the same callback method will be called. Functional slices can also have their independent life cycle changes. This independent change occurs during the ability slicing in the switching ability. However, such a switch will not affect the life cycle of the host's capabilities.

Capability slices have the following four life cycle states:

  • Initial: The capability slice has been loaded into memory, but it is not running. It is the initial state of all capability slices.
  • Inactive: The capability slice is loaded and executed, but is not interactive. Usually, it is an intermediate state before the capability slice becomes ACTIVE or Background. In this state, the UI of the functional slice may be visible, but input events cannot be received.
  • Activity: The ability slice is visible and interactive. Think that the capability slice has the focus.
  • Background: Capability slices are not visible. If the host ability is also in the background, the ability and ability slice will be destroyed in the case of insufficient memory.
    Similar to functions, function slices provide the following life cycle callback methods. You can overwrite them.
 
  public class MainAbilitySlice extends AbilitySlice {
      protected void onStart(Intent intent);
 
      protected void onActive();
 
      protected void onInactive();
 
      protected void onForeground(Intent intent);
 
      protected void onBackground();
 
      protected void onStop();
  }
 
 

The figure below shows the complete life cycle of a capability slice. A capability slice can only be in one state at any time. Not all states support direct transition.

image.png

Note: The onStart (ohos.aafwk.content.Intent) method can only be called once in the entire life cycle.

The meaning of each life cycle callback method and some best practice suggestions are provided below.

  • onStart (ohos.aafwk.content.Intent): This method must be implemented for UI initialization settings, such as setUIContent(int). This method can only be called once in the entire life cycle. The caller can use the Intent class to carry some custom keys and obtain these parameters through the methods provided by the intent.
  • onActive(): This method will be called when the function piece returns to the active state. After the callback, the UI is interactive.
  • onInactive(): This method will be called when the capability slice is completely or partially covered. After the callback, the UI is not interactive.
  • onForeground(ohos.aafwk.content.Intent)()}: This method will be called when the feature film returns to the foreground. The interaction logic of the application can be restored or initialized in the callback. If a new custom parameter needs to be specified when the ability slice returns to the foreground, the new intention parameter can be transmitted.
  • onBackground(): This method will be called when the capability slice enters the background. In this state, you cannot perform background tasks for a long time because the system may stop the background programs at any time.
  • onStop(): Call this method to destroy the capability slice. You can use this method to reclaim resources.
    Switching between capability slices
    You can use the present (ohos.aafwk.ability.AbilitySlice, ohos.aafwk.content.Intent) method to present a new capability slice, and use Intent to transmit custom parameters. Sample code:
 
     Button button = new Button(this);
     button.setClickedListener(listener -> {
         AbilitySlice targetSlice = new MyAbilitySlice();
         Intent intent = new Intent();
         intent.setParam("value", 10);
         present(targetSlice, intent);
     });
 

Note: A maximum of 1024 Ability Slices can be displayed in an Ability. If you try to display the 1025th ability slice, the system will force the ability to crash. To avoid this, if you need to display a large number of capability slices at the same time, you must improve the code.


Unauthorized reprinting is prohibited


For more technical exchanges, please join the QQ group

Group name: harmonyos Hongmeng Technology Exchange
Group number: 856567895


Start from scratch to learn HarmonyOS Hongmeng 2.0 development

Guess you like

Origin blog.csdn.net/iCloudEnd/article/details/108734793