Android back button is closing application
-
With the following code, pressing the back button on Android closes the application. I can see the "Accepted event" log message getting printed, but the app still closes. This is more important in a stack view where pressing the back button closes the application instead of going to the previous page.
import QtQuick.Controls import QtQuick import QtQuick.Layouts ApplicationWindow { id: window width: 400 height: 600 visible: true title: "Test" onClosing: { close.accepted = false console.log("Close cancelled") } Item { focus: true anchors.fill: parent Keys.onPressed: event => { if (event.key === Qt.Key_Back) { console.log("Accepted event") event.accepted = true } } Text { anchors.centerIn: parent text: "Hello" } } } -
Argument injection is no longer supported and my be the problem here use a lambda :
this code works fine in my appsWhat version of Qt and android are you targeting/using?
onClosing:(close)=> { //Management for Android Back-Button if(Qt.platform.os === "android"){ close.accepted = false } else { close.accepted = true } } -
What version of Qt
Qt 6.10.2
android are you targeting/using?
Are you referring to
ANDROID_PLATFORM? its set toandroid-28and I'm running it on a Pixel 4 simulator.I created a new project with the following code:
import QtQuick.Controls import QtQuick import QtQuick.Layouts ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Hello World") onClosing: close => { //Management for Android Back-Button if (Qt.platform.os === "android") { console.log("Accepting event") close.accepted = false } else { close.accepted = true } } } #include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("helloqml", "Main"); return app.exec(); }The
console.log()message is being printed, but the app still minimizes.ADB log:
D/qml : Accepting event D/VRI[QtActivity]: visibilityChanged oldVisibility=true newVisibility=false E/Surface : freeAllBuffers: 1 buffers were freed while being dequeued! E/Surface : freeAllBuffers: 1 buffers were freed while being dequeued! W/default : QRhiGles2: Failed to make context current. Expect bad things to happen. W/default : Failed to start frame W/WindowOnBackDispatcher: sendCancelIfRunning: isInProgress=false callback=android.app.Activity$$ExternalSyntheticLambda0@abe797f D/qml : Accepting event -
Looks like this is a bug in 6.10.2. It works with both 6.9.3 and 6.11.0
-
A account35467 has marked this topic as solved