Qt for HarmonyOS/qtohosextras doc/Application Continuation Example: Difference between revisions
Add code snippets to example pages (QTFOROH-2307) |
No edit summary |
||
| Line 1: | Line 1: | ||
= Application Continuation Example = | = Application Continuation Example = | ||
Revision as of 02:07, 17 September 2026
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.