DEV Community

SameX
SameX

Posted on

Analysis of the Free Flow Capability in HarmonyOS Next

Analysis of the Free Flow Capability in HarmonyOS Next

In the ecological system of HarmonyOS Next, the free flow capability is the key to achieving multi-device collaboration. It breaks down the barriers between devices, allowing users to enjoy a seamless operating experience across different devices. Today, let's conduct an in-depth analysis of this powerful capability.

Core Concepts of Free Flow (Cross-device Migration vs. Multi-device Collaboration)

Free flow is a general term for distributed operations across multiple devices in HarmonyOS Next, among which cross-device migration and multi-device collaboration are two important implementation forms.

Cross-device migration is like a "device journey" for an application. During the user's device usage process, once the usage context changes, for example, when returning from outdoors to indoors and finding that it is not very convenient to handle certain tasks on the mobile phone originally used, and the nearby tablet is more suitable, cross-device migration can be used to "move" the application running on the mobile phone to the tablet for continued use. From a development perspective, it means that the UIAbility running on device A is migrated to device B, and the UIAbility on device A can choose whether to exit according to actual needs. For example, when watching a video on a mobile phone, after arriving home, the video can be migrated to a smart screen for continued playback. The video's playback progress, playback settings, etc. can all be continued intact, bringing users a seamless viewing experience.

Multi-device collaboration is more like a "team collaboration" between devices. Multiple devices act as a whole, jointly providing users with more efficient and immersive services than a single device. Different UIAbilities or ServiceExtensionAbilities on multiple devices can run simultaneously or alternately to complete the entire business. For example, opening the same note in the memo on two devices simultaneously for collaborative editing. Device A can insert pictures from the local photo gallery, and device B is responsible for editing the text content. Different devices perform their respective functions, greatly improving the user's editing efficiency and experience.

Analysis of Typical Application Scenarios

  1. Cross-device Email Editing: This is a typical application of cross-device migration. When a user composes an email on a mobile phone and then switches to a computer to continue editing midway. The free flow capability of HarmonyOS Next will save the editing state of the email, including the entered content, inserted attachments, and set formats, etc. When opening the email application on the computer, this information will be completely restored, as if the user has been editing on the computer all the time. During development, through the application continuation technology, the email data is saved in the onContinue() interface of the source-side UIAbility, and the data is restored in the onCreate() or onNewWant() interface of the target side to achieve seamless switching of email editing.
  2. Multi-device Collaborative Fitness: In this scenario, multi-device collaboration is fully demonstrated. The user wears a smart watch for exercise, and the watch collects data such as heart rate and step count in real time. At the same time, the fitness application on the mobile phone or tablet receives this data and provides personalized exercise suggestions based on the data, and displays exercise course videos, etc. The mobile phone can also serve as a control center, making it convenient for users to adjust their fitness plans. During the development process, it is necessary to achieve real-time data transmission and interaction between different devices to ensure that the applications on each device can work collaboratively to provide users with a comprehensive fitness experience.
  3. Multi-screen Gaming: Multi-screen gaming fully showcases the charm of free flow. Take an adventure game as an example. The player conducts the early exploration and operation of the game on a mobile phone. After connecting to a large-screen TV, the game screen automatically switches to the TV. With the large screen and high resolution of the TV, the player can obtain a more shocking visual experience. At this time, the mobile phone can be used as a game controller, and precise control can be achieved by using its sensors. In this process, cross-device migration realizes the seamless switching of the game screen and operation, and multi-device collaboration enables the mobile phone and the TV to jointly create an immersive gaming experience for the player. Developers need to solve problems such as graphic synchronization between devices, operation instruction transmission, and performance optimization to ensure that the game can run smoothly on different devices.

Practical Case: How to Freely Switch an Application among Multiple Devices

The following is a simple example of a text editor application to show how to achieve the free switching of an application among multiple devices.

Configure the Application Continuation Capability

In the abilities of the module.json5 file, configure the continuable tag as true, indicating that this UIAbility can be migrated:

{
    "module": {
        "abilities": [
            {
                "continuable": true
            }
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

Save Data on the Source Side

Implement the onContinue() interface in the source-side UIAbility to save the content in the text editor:

import { AbilityConstant, UIAbility } from '@kit.AbilityKit';

export default class TextEditorAbility extends UIAbility {
    onContinue(wantParam: Record<string, Object>) {
        // Assume the function to get the content of the text editor is getEditorContent
        let editorContent = getEditorContent();
        wantParam["editorContent"] = editorContent;
        return AbilityConstant.OnContinueResult.AGREE;
    }
}
Enter fullscreen mode Exit fullscreen mode

Restore Data on the Target Side

Implement the onCreate() or onNewWant() interface in the target-side UIAbility to restore the content of the text editor:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';

export default class TextEditorAbility extends UIAbility {
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
        if (launchParam.launchReason === AbilityConstant.LaunchReason.CONTINUATION) {
            let editorContent = want.parameters?.editorContent;
            if (editorContent) {
                // Assume the function to set the content of the text editor is setEditorContent
                setEditorContent(editorContent);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Through the above steps, the free switching of a simple text editor application among multiple devices can be achieved. When the user opens the application on different devices, they can continue their previous editing work. Of course, in actual application development, more details need to be considered, such as data consistency, device compatibility, etc. But this example provides a basic implementation idea for everyone.

The free flow capability of HarmonyOS Next provides developers with broad space for innovation. By reasonably using cross-device migration and multi-device collaboration technologies, we can create more intelligent and efficient multi-device applications, bringing users an unprecedented convenient experience. I hope that everyone can make full use of this capability in actual development to create more excellent applications.

Top comments (0)