Jump to content

Qt for HarmonyOS/qtohosextras doc/Application Continuation Example: Difference between revisions

From Qt Wiki
No edit summary
Publish example source listings and restore Files lists (QTFOROH-2307)
Line 1: Line 1:
< [[Qt_for_HarmonyOS/qtohosextras_doc|Qt for HarmonyOS / qtohosextras doc]]
= Application Continuation Example =
= Application Continuation Example =


Line 65: Line 67:
'''Note: '''The window is shown before QOhosAbilityContext::getInstanceForMainWindow() is called, so that a valid [https://doc.qt.io/qt-5/qwindow.html QWindow] exists. The restored distributed data object handle is kept alive for as long as the fields should stay in sync, and assets are attached before the object joins a distributed session.
'''Note: '''The window is shown before QOhosAbilityContext::getInstanceForMainWindow() is called, so that a valid [https://doc.qt.io/qt-5/qwindow.html QWindow] exists. The restored distributed data object handle is kept alive for as long as the fields should stay in sync, and assets are attached before the object joins a distributed session.


* [[Qt_for_HarmonyOS/qtohosextras_doc/Application_Continuation_Example|applicationcontinuation/main.cpp]]
Files:
 
* [[Qt_for_HarmonyOS/qtohosextras_doc/Application_Continuation_Example_-_applicationcontinuation.pro|applicationcontinuation/applicationcontinuation.pro]]
* [[Qt_for_HarmonyOS/qtohosextras_doc/Application_Continuation_Example_-_main.cpp|applicationcontinuation/main.cpp]]


[[Category:Qt for HarmonyOS]]
[[Category:Qt for HarmonyOS]]

Revision as of 02:27, 17 September 2026

< Qt for HarmonyOS / qtohosextras doc

Application Continuation Example

Demonstrates cross-device application continuation with distributed data objects and file assets.


The window shows a set of editable fields, the content of a text file asset, a sync progress label, a checkbox controlling whether the source device exits after migration, and buttons that toggle the continuation state with QOhosAbilityContext::setContinuationActive().

On the source device, the continueRequestReceived() signal delivers a QOhosOnContinueContext. The example builds a QOhosDistributedDataObject from the field values with QOhosDistributedDataObject::create(), attaches the text file with addAssets(), and hands the object over with QOhosOnContinueContext::setResponseOnDistributedDataObjectsSaveResults(). When saving succeeded and QOhosOnContinueContext::sourceApplicationVersionCode() matches the local version code, the request is accepted with setAgreeResponse(); otherwise setMismatchResponse() is called. The checkbox state is applied with setExitAppOnSourceDeviceAfterMigration() before the response is delivered.

     QObject::connect(
         ability.get(), &QtOhosExtras::QOhosUiAbilityContext::continueRequestReceived,
         ability.get(),
         [fields, exitAfterMigrationCheckBox, abilityPtr = ability.get()](auto context) {
             auto *appContext = QtOhosExtras::QOhosAppContext::instance();
             auto appVersionCode = appContext->getBundleInfo()->versionCode();

             context->setExitAppOnSourceDeviceAfterMigration(exitAfterMigrationCheckBox->isChecked());

             auto object = createObjectToMigrate(fields);
             context->setResponseOnDistributedDataObjectsSaveResults(
                 {object},
                 abilityPtr,
                 [context, appVersionCode](const QList<bool> &results) {
                     const bool saved = !results.isEmpty() && !results.contains(false);
                     if (saved && context->sourceApplicationVersionCode() == appVersionCode) {
                         context->setAgreeResponse(QByteArray("Migration Test Data"));
                     } else {
                         context->setMismatchResponse();
                     }
                 });
         });

On the target device, QtOhosExtras::tryGetOnContinueData() and QtOhosExtras::tryRestoreOnContinueDistributedDataObjects() recover the migrated data. The restored object's dataChanged(), statusChanged() and progressChanged() signals keep the fields, the asset view and the progress label up to date.

The launch want is inspected at startup for migrated continuation data:

     auto launchWant = QtOhosExtras::QOhosAppContext::getAppLaunchWant();
     auto onContinueData = QtOhosExtras::tryGetOnContinueData(launchWant);
     if (onContinueData) {
         valueLabel->setText(*onContinueData.get());
     } else {
         valueLabel->setText(QLatin1String(""));
     }

When a new want arrives while the application is already running, the distributed data objects are restored from it:

             auto newWantContinuationObjectHandle = restoreFirstObject(
                 QtOhosExtras::tryRestoreOnContinueDistributedDataObjects(wantInfo->want()), fields);
             if (newWantContinuationObjectHandle)
                 continuationObjectHandle = newWantContinuationObjectHandle;

Note: The window is shown before QOhosAbilityContext::getInstanceForMainWindow() is called, so that a valid QWindow exists. The restored distributed data object handle is kept alive for as long as the fields should stay in sync, and assets are attached before the object joins a distributed session.

Files: