Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Android back button is closing application

Android back button is closing application

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 2 Posters 148 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    account35467
    wrote last edited by
    #1

    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"
            }
        }
    }
    
    
    1 Reply Last reply
    0
    • J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote last edited by
      #2

      Argument injection is no longer supported and my be the problem here use a lambda :
      this code works fine in my apps

      What 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
               }
          }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        account35467
        wrote last edited by
        #3

        What version of Qt

        Qt 6.10.2

        android are you targeting/using?

        Are you referring to ANDROID_PLATFORM? its set to android-28 and 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
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          account35467
          wrote last edited by
          #4

          Looks like this is a bug in 6.10.2. It works with both 6.9.3 and 6.11.0

          1 Reply Last reply
          2
          • A account35467 has marked this topic as solved

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved