[Hongmeng] Page Ability Life Cycle of HarMonyOS

Actions such as system administration or user actions cause Page instances to transition between different states of their [lifecycle .] The callback mechanism provided by the Ability class enables Page to sense external changes in time, so as to correctly respond to state changes (such as releasing resources), which helps to improve the performance and robustness of the application.

Page lifecycle [callbacks]

The different state transitions of the Page life cycle and their corresponding callbacks are shown in Figure 1.

Figure 1  Page life cycle

  • onStart()

    This callback is fired when the system first creates a Page instance. For a Page instance, the callback is fired only once during its life cycle, and the Page will enter the INACTIVE state after this logic. Developers must override this method and configure the default displayed AbilitySlice here.

    @Override
    public void onStart(Intent intent) {
    super.onStart(intent);
    super.setMainRoute(FooSlice.class.getName());
    }

  • onActive()

    Page will come to the foreground after entering the INACTIVE state, and then the system will call this callback. After this, the Page enters the ACTIVE state, which is the state in which the application interacts with the user. The Page will remain in this state unless some kind of event occurs that causes the Page to lose focus, such as the user hitting the back key or navigating to another Page. When such an event occurs, it will trigger the Page to return to the INACTIVE state, and the system will call the onInactive() callback. After that, the Page may return to the ACTIVE state, and the system will call the onActive() callback again. Therefore, developers usually need to implement onActive() and onInactive() in pairs, and acquire resources released in onInactive() in onActive().

  • onInactive()

    The system calls this callback when the Page loses focus, after which the Page enters the INACTIVE state. Developers can implement the appropriate behavior when the Page loses focus in this callback.

  • onBackground()

    If the Page is no longer visible to the user, the system will call this callback to notify the developer and the user to release the corresponding resources, after which the Page enters the BACKGROUND state. Developers should release resources that are useless when the Page is not visible in this callback, or perform time-consuming state saving operations in this callback.

  • onForeground()

    The Page in the BACKGROUND state still resides in memory. When it returns to the foreground (for example, the user re-navigates to this Page), the system will first call the onForeground() callback to notify the developer, and then the life cycle state of the Page returns to the INACTIVE state . The developer should re-apply for the resources released in onBackground() in this callback, and finally the life cycle state of the Page further returns to the ACTIVE state, and the system will notify the developer and user through the onActive() callback.

  • onStop()

    When the system is about to destroy the Page, this callback function will be triggered to notify the user to release system resources. Possible reasons for destroying a Page include the following:

    • The user closes the specified Page through the system management capability, for example, using the Task Manager to close the Page.
    • A user action triggers the Page’s terminateAbility() method call, such as using the app’s exit feature.
    • Configuration changes cause the system to temporarily destroy the Page and rebuild it.
    • For the purpose of resource management, the system automatically triggers the destruction of the Page in the BACKGROUND state.

AbilitySlice Lifecycle

As the constituent unit of Page, AbilitySlice’s life cycle depends on the life cycle of the Page it belongs to. AbilitySlice and Page have the same lifecycle state and callback with the same name, and when the Page’s lifecycle changes, its AbilitySlice will also have the same lifecycle changes. In addition, AbilitySlices also have Page-independent lifecycle changes, which occur when navigating between AbilitySlices within the same Page, without the Page’s lifecycle state changing.

AbilitySlice lifecycle callbacks are similar to Page’s corresponding callbacks, so I won’t repeat them here. Since the AbilitySlice carries a specific page, the developer must override the onStart() callback of the AbilitySlice and set the page through the setUIContent() method in this method, as follows:

@Override
protected void onStart(Intent intent) {
super.onStart(intent);

setUIContent(ResourceTable.Layout_main_layout);
}

The creation and management of AbilitySlice instances is usually the responsibility of the application, and the system only creates AbilitySlice instances in certain situations. For example, when an AbilitySlice is launched through navigation, the system is responsible for instantiation; but when navigating between different AbilitySlices in the same Page, the application is responsible for instantiation.

Page is associated with the AbilitySlice lifecycle

When an AbilitySlice is in the foreground and has focus, its lifecycle state changes with the lifecycle state of the page it belongs to. When a Page has multiple AbilitySlices, for example: MyAbility has FooAbilitySlice and BarAbilitySlice, the current FooAbilitySlice is in the foreground and gets the focus, and is about to navigate to BarAbilitySlice, the life cycle state change sequence during this period is:

  1. FooAbilitySlice changes from ACTIVE state to INACTIVE state.
  2. The BarAbilitySlice then goes from the INITIAL state first to the INACTIVE state and then to the ACTIVE state (assuming the BarAbilitySlice has not been started before).
  3. FooAbilitySlice changes from INACTIVE state to BACKGROUND state.

The callback sequence of the lifecycle methods corresponding to the two slices is:

FooAbilitySlice.onInactive() –> BarAbilitySlice.onStart() –> BarAbilitySlice.onActive() –> FooAbilitySlice.onBackground()

Throughout the process, MyAbility is always in the ACTIVE state. However, when the Page is destroyed by the system, all its instantiated AbilitySlices will be destroyed together, not just the AbilitySlice in the foreground.

Understand the idea:

If you have a certain android-based friend, you may be able to understand and discover quickly:

Comparison of Hongmeng Ability and Android Activity
Hongmeng android
Ability Activity
AbilitySlice Fragment
An Ability can contain multiple AbilitySlices An Activity can contain multiple Fragments
onStart() onCreate()
onActive() onResume()
onInActive() on Pause
onBackground() onStop()
onForeground() onRestart()
onStop() onDestory()

Leave a Comment

Your email address will not be published. Required fields are marked *