DEV Community

SameX
SameX

Posted on

Development Guide for Application Continuation in HarmonyOS Next

Development Guide for Application Continuation in HarmonyOS Next

Hello, everyone! As a developer who has been deeply involved in the field of Huawei HarmonyOS development for many years, today I'd like to share with you some knowledge related to the development of application continuation in HarmonyOS Next. The application continuation function provides users with the experience of seamlessly switching devices while using an application, greatly enhancing the convenience of users' operations among multiple devices. Next, let's have an in-depth discussion about the specific content of application continuation.

Basic Concepts and Application Scenarios of Application Continuation

Application continuation, simply put, means that when a user operates a certain application on one device, they can quickly switch to another device and continue the task of that application, achieving a seamless usage experience. This function involves distributed state synchronization technology behind it, which ensures that when the application migrates between different devices, the user's data and operation state can be accurately saved and restored.

In practical usage scenarios, application continuation plays an important role. For example, when you are browsing a long article on your mobile phone and suddenly need to use a tablet for a more comfortable reading experience, the application continuation function allows you to continue reading from the position where you left off on the mobile phone on the tablet. The scrolling position of the browser page, the reading progress, etc. can all be perfectly synchronized. Another example is when you are handling a document editing task on a PC and need to go out, you can continue editing on your mobile phone based on the progress on the PC. The content in the editor, the format settings, etc. will not be lost. Such scenarios of switching from a mobile phone to a tablet or continuing a task from a PC on a mobile phone greatly improve the user's work and life efficiency, making the collaborative use of multiple devices more smooth and natural.

Analysis of Application Continuation APIs

onContinue()

onContinue() is a key interface of the source-side UIAbility in application continuation. When the application triggers a migration, this interface will be called on the source side. Developers can complete several important operations in this interface, such as saving the data required for the migration. You can save the key information of the current application, such as the text content in the editor, the current page URL of the browser, etc., in the wantParam in the form of key-value pairs. At the same time, application compatibility detection can also be carried out. Obtain the version number of the application on the opposite end of the migration from the wantParam.version in the input parameters of onContinue(), and compare it with the version number of the source-side application to determine whether this migration is supported. If the versions are not compatible, corresponding error information can be returned to prompt the user. The sample code is as follows:

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

export default class EntryAbility extends UIAbility {
    onContinue(wantParam: Record<string, Object>) {
        let targetVersion = wantParam.version; 
        let versionThreshold: number = 1; 
        if (targetVersion < versionThreshold) {
            promptAction.showToast({
                message: 'The version number of the target application is too low to support continuation. Please upgrade the application version and try again.',
                duration: 2000
            });
            return AbilityConstant.OnContinueResult.MISMATCH;
        }
        let continueInput = 'Migrated data';
        if (continueInput) {
            wantParam["data"] = continueInput;
        }
        return AbilityConstant.OnContinueResult.AGREE;
    }
}
Enter fullscreen mode Exit fullscreen mode

onCreate()

On the target device, the onCreate() interface is used to restore the migrated data. Determine whether this startup is a migration startup through launchReason. If so, you can obtain the saved migrated data from want. For example:

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

export default class EntryAbility extends UIAbility {
    storage: LocalStorage = new LocalStorage();
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
        if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) {
            let continueInput = '';
            if (want.parameters != undefined) {
                continueInput = JSON.stringify(want.parameters.data);
                console.info(`continue input ${continueInput}`);
            }
            this.context.restoreWindowStage(this.storage);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

onNewWant()

For single-instance applications, it is also necessary to additionally implement the onNewWant() interface. Its implementation method is similar to that of onCreate(). That is, when it is determined to be a migration scenario, the data is restored and the page restoration is triggered. The sample code is as follows:

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

export default class EntryAbility extends UIAbility {
    storage: LocalStorage = new LocalStorage();
    onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
        console.info(`EntryAbility onNewWant ${AbilityConstant.LaunchReason.CONTINUATION}`);
        if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) {
            let continueInput = '';
            if (want.parameters != undefined) {
                continueInput = JSON.stringify(want.parameters.data);
                console.info(`continue input ${continueInput}`);
            }
            this.context.restoreWindowStage(this.storage);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)