Save on Exit on Android
-
In my Qt based application I save some downloads at shutdown.
I do that by connecting to QCoreApplication::aboutToQuit and that signal handler won't return until the save has completed.
This works just fine on my Linux desktop build.On Android, to my surprise, the code doesn't complete and the file is only saved partially. I triple checked that this is not due to some assert or whatever being thrown, and after I put a log line in my save-loop I noticed that every time it actually stops saving after a different number of iterations (typically less than 10 out of 30000).
My guess is that the aboutToQuit signal is interrupted after 20 milliseconds or so and a hard kill is executed for my application.
I there a way to get an earlier notification of shutdown where I can safely save?
-
In my Qt based application I save some downloads at shutdown.
I do that by connecting to QCoreApplication::aboutToQuit and that signal handler won't return until the save has completed.
This works just fine on my Linux desktop build.On Android, to my surprise, the code doesn't complete and the file is only saved partially. I triple checked that this is not due to some assert or whatever being thrown, and after I put a log line in my save-loop I noticed that every time it actually stops saving after a different number of iterations (typically less than 10 out of 30000).
My guess is that the aboutToQuit signal is interrupted after 20 milliseconds or so and a hard kill is executed for my application.
I there a way to get an earlier notification of shutdown where I can safely save?
@TomZ
Googlingqt android abouttoquit
shows you others have problems, indeed some claimaboutToQuit()
is not called at all! Various hits, one accepted solutuon at https://stackoverflow.com/a/55416311Use QGuiApplication::applicationStateChanged to save application's states.
In the case of Windows, clean up the resource with: QApplication::aboutToQuit
In the case of Android, let the system clean up the resource.
Suggestion is that
QGuiApplication::applicationStateChanged
is what to use under Android?Mind, examples seem to only be doing "clean up" (quick?) rather than a "save file" (slow?). Might be that this still falls foul of some Android timeout...
-
@JonB said in Save on Exit on Android:
Suggestion is that QGuiApplication::applicationStateChanged is what to use under Android
Awesome, tested this and indeed the way to go.
I'll add a "save" method to call on reaching mode 'inactive' which should keep the shutdown to do almost nothing.
-
@JonB said in Save on Exit on Android:
Suggestion is that QGuiApplication::applicationStateChanged is what to use under Android
Awesome, tested this and indeed the way to go.
I'll add a "save" method to call on reaching mode 'inactive' which should keep the shutdown to do almost nothing.
@TomZ I'm doing something similar:
// iOS: Attention: NO SIGNAL AboutToQuit // Android: SIGNAL if leaving the App with Android BACK Key // Android: NO SIGNAL if using HOME or OVERVIEW and THEN CLOSE from there void ApplicationUI::onAboutToQuit() { qDebug() << "On About to Q U I T Signal received"; startCaching(); } void ApplicationUI::onApplicationStateChanged(Qt::ApplicationState applicationState) { qDebug() << "S T A T E changed into: " << applicationState; // Hint: suspend not from macOS - only Android, iOS if(applicationState == Qt::ApplicationState::ApplicationSuspended) { startCaching(); } }
FYI: I'm storing my app data as JSON files. Can be some MB.
Doing caching not only if closing the app, but also if moving to background. works well for my customers.
(And if app is running in KIOSK mode on Android, I'm caching in intervals using a Timer)