I'm developing a mobile application for Android using Qt. I've read this question about connecting to QApplication::aboutToQuit
and/or QApplication::lastWindowClosed
in order to do last-minute cleanup. However, I have found these methods insufficient when the user swipes to clear an application from the multitasking menu in Android. In this case it seems neither signal is broadcast and my application data is left in an inappropriate state. Is there any way to ensure that cleanup is done regardless of how the app is closed?
I've set up the following test code to verify they're not being broadcast:
#include <QGuiApplication>
#include <QDebug>
class Application : public QGuiApplication
{
Q_OBJECT
public:
Application(int &argc, char **argv, int flags = ApplicationFlags)
: QGuiApplication(argc, argv, flags) {
connect(this, &QGuiApplication::aboutToQuit,
&Application::notifyQuitting);
connect(this, &QGuiApplication::lastWindowClosed,
&Application::notifyLastWindowClosed);
}
static void notifyQuitting() {
qDebug() << "quitting!";
}
static void notifyLastWindowClosed() {
qDebug() << "LastWindowClosed!";
}
};
Update:
I've implemented a custom Activity in Java and found that it seems that all three of onPause
, onStop
, and onDestroy
are called whenever the application is killed in this manner. Of course, onStop
and onDestroy
may never be called if the device is low on memory, so the only surefire way to do it is in onPause
.
It also seems that Qt has the signal QGuiApplication::applicationStateChanged
, though I haven't looked into this much.