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. catch Android back button from navigation bar
QtWS25 Last Chance

catch Android back button from navigation bar

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 3 Posters 941 Views
  • 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
    ankou29666
    wrote on 2 Oct 2024, 10:37 last edited by
    #2

    Hi

    are you sure that the onReleased event is the one you need ?
    have you tried other events like key pressed to see what happens ?

    1 Reply Last reply
    0
    • S shokarta
      2 Oct 2024, 10:29

      Hello,

      I am trying to manage the back button action in QML, so I have tried:

      Window {
          id: root
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      	
      	Rectangle {
      		id: main
      		focus: true // important - otherwise we'll get no key events
      		Component.onCompleted: main. forceActiveFocus()
      
      		Keys.onReleased: {
      			console.log("event key:", event.key);
      			if (event.key == Qt.Key_Back) {
      				console.log("Back button captured!");
      				event.accepted = true;
      			}
      		}
      	}
      }
      

      but it does not do anything, the back button kills the application, does not even console.log "event key".
      so as far as I assume the back button in navigation bar is just hardware keyboard, then this shall work?
      also, on the other hand i have made lots of manual modificaitons on my android emulator, so its possible i did disable this :D
      can anyone try on real device please? or tell me if my approach is actually good?

      Thank you

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 2 Oct 2024, 10:54 last edited by
      #3

      @shokarta

      release and pressed are all different signals, its possible that the uncaptured pressed key event is enough for the application to close

      The way I do it in my applications is by intercepting the close event instead:

      onClosing: {
          //Management for Android Back-Button
          if(Qt.platform.os === "android"){
              close.accepted = false
              if(PageManager.pageIndex > 0)
                  PageManager.pageIndex = 0
          }
      }
      

      but thats based on really old legacy code, so the key event route may be the more correct way to do it :D


      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
      2
      • A Offline
        A Offline
        ankou29666
        wrote on 2 Oct 2024, 10:57 last edited by
        #4

        @J-Hilk According to 6.7.3's Window documentation, your way is still the appropriate one to handle such case.

        1 Reply Last reply
        2
        • S Offline
          S Offline
          shokarta
          wrote on 2 Oct 2024, 12:16 last edited by
          #5

          hmm very weird, i tried:

          Window {
          	id: root
          
          	// Management for Android Back-Button
          	onClosing: {
          		console.log("debug1");
          		if (Qt.platform.os === "android" || Qt.platform.os === "ios") {
          			console.log("debug2");
          			close.accepted = false;
          			console.log("debug3");
          		}
          		else { console.log("debug4"); close.accepted = true; }
          	}
          }
          

          and I got:

          W WindowOnBackDispatcher: sendCancelIfRunning: isInProgress=falsecallback=android.app.Activity$$ExternalSyntheticLambda0@cd8e98f    // thsi is OK behaviour for back button on Android, so all good
          D qml     : : debug1
          D qml     : : debug2
          W qt.qml.context: : qrc:/Main.qml:23:2 Parameter "close" is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead.   // this is line with "onClosing: {"
          D qml     : : debug3 // should this also be printed to console? its after close event
          

          what do i do wrong?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shokarta
            wrote on 2 Oct 2024, 12:29 last edited by
            #6

            and according to:
            https://stackoverflow.com/questions/47175409/qml-asking-confirmation-before-closing-application (last post)
            and https://www.direktembedded.com/qt5-to-qt6-qml-pyside-porting/

            the following still does NOT work :(

            onClosing: function(close) {
            		console.log("debug1");
            		if (Qt.platform.os === "android" || Qt.platform.os === "ios") {
            			console.log("debug2");
            			close.accepted = false;
            			console.log("debug3");
            		}
            		else { console.log("debug4"); close.accepted = true; }
            	}
            
            W WindowOnBackDispatcher: sendCancelIfRunning: isInProgress=falsecallback=android.app.Activity$$ExternalSyntheticLambda0@cd303a8
            D qml     : : debug1
            D qml     : : debug2
            D qml     : : debug3
            

            and closes anyway

            1 Reply Last reply
            0
            • S Offline
              S Offline
              shokarta
              wrote on 2 Oct 2024, 12:47 last edited by
              #7

              hmm, even the official docu:
              6e49b67d-193e-4281-9744-51c5da03984b-image.png
              is kinda weird...

              i found out that the app is realy not killed, the debugger stays active, but the app gets minimized, and when i try to bring it up, it freezes... i guess this I will report as bug

              1 Reply Last reply
              0
              • S Offline
                S Offline
                shokarta
                wrote on 2 Oct 2024, 13:04 last edited by
                #8
                This post is deleted!
                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ankou29666
                  wrote on 2 Oct 2024, 18:06 last edited by ankou29666 10 Feb 2024, 18:06
                  #9

                  writing

                      onClosing: (close) => // or function (close), they're equivalent
                      {
                          console.log ("closing window") ;
                          close.accepted = false ;
                      }
                  

                  works fine, and I have to forcefully close the app. I've only tested on windows, not on android.

                  However Qt Creator pisses me of with an "Invalid property name 'onClosing'" error

                  Writing

                      onClosing:
                      {
                          console.log ("closing window") ;
                          close.accepted = false ;
                      }
                  

                  works the same, I still have to forcefully quit, but produces me the same deprecation warning than you posted earlier.

                  At the origins of QtQuick, there had been criticism again the fact that slots didn't make their parameters explicit, making the parameters look like coming out of nowhere. Thus the newer syntax on your screenshot.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    shokarta
                    wrote on 3 Oct 2024, 07:43 last edited by
                    #10

                    works fine on Windows,
                    but not on Android... at least emulator... is there any chance you could try yourself? emulator, real device, anything?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      shokarta
                      wrote on 4 Oct 2024, 16:28 last edited by shokarta 10 Apr 2024, 17:03
                      #11

                      Solution found...
                      in android manifest, in <application> i must deactivate enableOnBackInvokedCallback, so correct is:

                      android:enableOnBackInvokedCallback="false"
                      

                      from this point, all works as expected
                      seems as new thing from android 13 according to google search

                      1 Reply Last reply
                      2
                      • S shokarta has marked this topic as solved on 4 Oct 2024, 17:03

                      11/11

                      4 Oct 2024, 16:28

                      • Login

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