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. Trouble with back-button an android
QtWS25 Last Chance

Trouble with back-button an android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
24 Posts 2 Posters 3.9k 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.
  • V Offline
    V Offline
    Vega4
    wrote on 24 Aug 2021, 07:53 last edited by Vega4
    #1

    Re: Android backbutton

    ok folks seems like we've tried everything....

    • tried the in QML onClosing() event - never gets fired.
    onCLosing: {
    close.accepted=true;
    }
    
    • tried platform specific Java implementation in activity with sendToBack
    @Override
    public void onBackPressed() {
     moveTaskToBack(true);
    }
    
    • tried implementing an Event Filter... registering it with tha main window... no luck.. no event caught for back button.... then plugged the event into the Application itself... no luck... lots of events caught by the filter.. not a single one for the back_button.

    • so we got pissed off and overrided notify() within QApplicaiton itself.. just to see.. that not events for back button are being dispatched at all?

    class FilteredApplication final : public QApplication
    {
    public:
        FilteredApplication(int &argc, char **argv) : QApplication(argc, argv) {}
        virtual bool notify(QObject *receiver, QEvent *event) override
        {
            if (event->type() == QEvent::KeyPress) {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                int key = keyEvent->key();
    
                        if(key == Qt::Key_Back)
                         {
                            qDebug("Ate a back-button press.", keyEvent->key());
    
                            CUIActions * ui = CUIActions::getInstance();
                            if(ui)
                            {
                                if(ui->areAnyDialogsOpen())
                                {
                                    ui->closeAllDialogsRequest();
                                }
                                else
                                {
                                   exit(0);
                                }
    
                            }
                            return true;
                         }
            }
           return QApplication::notify(receiver,event);
        }
    };
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 24 Aug 2021, 08:06 last edited by J.Hilk
      #2

      @Vega4 said in Trouble with back-button an android:

      onCLosing: {
      close.accepted=true;
      }

      That method works for me, to be fair.

      ApplicationWindow {
          id: root
      onClosing:{
              //Manages Android Back-button
              if(Qt.platform.os == "android"){
                  close.accepted = false
              }
          }
      ....
      ...
      }
      

      The onClose has to be on the very root element of your gui tree, the one you load in main.cpp

      I don't have to typo and I set the accepted to false instead of true... maybe thats the issue ?

      For some reason, the key events get to the QML side before you see them in QApplication and the Eventfilter, I noticed that too 🤷‍♂️


      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.

      V 1 Reply Last reply 24 Aug 2021, 08:10
      1
      • V Offline
        V Offline
        Vega4
        wrote on 24 Aug 2021, 08:07 last edited by
        #3

        .. an interesting thing to note... the notify() override gets executed... ONLY and when a custom QDialog gets opened()... which is good as handling of back_arrow key on android when it comes to QDialogs is uterly broken and we at least have a chance of doing some cleaning up by hand. By default QDialog.. is hidden.. not destroyed not anything which used to cause A LOT of trouble.

        Still no events no notifications when the main (full-screen) window is open. Nothing.

        1 Reply Last reply
        0
        • J J.Hilk
          24 Aug 2021, 08:06

          @Vega4 said in Trouble with back-button an android:

          onCLosing: {
          close.accepted=true;
          }

          That method works for me, to be fair.

          ApplicationWindow {
              id: root
          onClosing:{
                  //Manages Android Back-button
                  if(Qt.platform.os == "android"){
                      close.accepted = false
                  }
              }
          ....
          ...
          }
          

          The onClose has to be on the very root element of your gui tree, the one you load in main.cpp

          I don't have to typo and I set the accepted to false instead of true... maybe thats the issue ?

          For some reason, the key events get to the QML side before you see them in QApplication and the Eventfilter, I noticed that too 🤷‍♂️

          V Offline
          V Offline
          Vega4
          wrote on 24 Aug 2021, 08:10 last edited by Vega4
          #4

          @J-Hilk

          ApplicationWindow
          {
          
              id: window
              flags:  Qt.WindowTitleHint  |Qt.WA_TranslucentBackground|(Qt.platform.os === "ios" ? Qt.MaximizeUsingFullscreenGeometryHint : 0) &(~Qt.WA_ContentsMarginsRespectsSafeArea)
              visibility: Window.FullScreen
              color: "#00000000"
          
              onClosing: {
                  close.accepted = true;
              }
          
          

          that's how it is on our end.. yeah.. we realy wish THIS would have worked indeed:D It doesn't.

          J 1 Reply Last reply 24 Aug 2021, 08:13
          0
          • V Vega4
            24 Aug 2021, 08:10

            @J-Hilk

            ApplicationWindow
            {
            
                id: window
                flags:  Qt.WindowTitleHint  |Qt.WA_TranslucentBackground|(Qt.platform.os === "ios" ? Qt.MaximizeUsingFullscreenGeometryHint : 0) &(~Qt.WA_ContentsMarginsRespectsSafeArea)
                visibility: Window.FullScreen
                color: "#00000000"
            
                onClosing: {
                    close.accepted = true;
                }
            
            

            that's how it is on our end.. yeah.. we realy wish THIS would have worked indeed:D It doesn't.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 24 Aug 2021, 08:13 last edited by
            #5

            @Vega4 said in Trouble with back-button an android:

            true

            thats still a true not a false...


            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.

            V 1 Reply Last reply 24 Aug 2021, 08:18
            0
            • V Offline
              V Offline
              Vega4
              wrote on 24 Aug 2021, 08:14 last edited by
              #6

              (..) on top of everything isn't it strange that both the Application-level and window-level event-filters do not trigger... that the QApplicaiton's notify() overrides do not get triggered ? Aren't these the very things that dispatch events to anything else lower in the hierarchy?

              1 Reply Last reply
              0
              • J J.Hilk
                24 Aug 2021, 08:13

                @Vega4 said in Trouble with back-button an android:

                true

                thats still a true not a false...

                V Offline
                V Offline
                Vega4
                wrote on 24 Aug 2021, 08:18 last edited by Vega4
                #7

                @J-Hilk
                What do you mean? we want the window to actually CLOSE .. or be sent to the back to be precise. The thing is nothing happens.
                That's why we set it to true (i.e. accept)

                J 1 Reply Last reply 24 Aug 2021, 08:20
                0
                • V Vega4
                  24 Aug 2021, 08:18

                  @J-Hilk
                  What do you mean? we want the window to actually CLOSE .. or be sent to the back to be precise. The thing is nothing happens.
                  That's why we set it to true (i.e. accept)

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 24 Aug 2021, 08:20 last edited by
                  #8

                  @Vega4 ok you need to actually tell me what is it you actually want to archive here.

                  You originally forked from here:
                  https://forum.qt.io/topic/28801/android-backbutton

                  I'm making simple examples with Qt 5.1 for android, but i have noted that with the backbutton the app exit instead of come back at previous stack page. Will be it also in future version? I think that is very important to have native behaviour (without trick).

                  which is about preventing the android back button from closing the whole application...


                  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.

                  V 1 Reply Last reply 24 Aug 2021, 08:22
                  0
                  • J J.Hilk
                    24 Aug 2021, 08:20

                    @Vega4 ok you need to actually tell me what is it you actually want to archive here.

                    You originally forked from here:
                    https://forum.qt.io/topic/28801/android-backbutton

                    I'm making simple examples with Qt 5.1 for android, but i have noted that with the backbutton the app exit instead of come back at previous stack page. Will be it also in future version? I think that is very important to have native behaviour (without trick).

                    which is about preventing the android back button from closing the whole application...

                    V Offline
                    V Offline
                    Vega4
                    wrote on 24 Aug 2021, 08:22 last edited by Vega4
                    #9

                    @J-Hilk
                    Seems like we want the application to actually close. Like we want the opposite. Minimize or anything or to have control over the process but like described above. for anything we do we are unable to intercept the key_back event.

                    That's what we want - to intercept the key-back event since we need to do some cleaning up before sending the app to the back.

                    There must be some reason for which the app is completely unaware of the back-keyt presses which is why.. it is not minimized in the first place (which should be by default right?) maybe because it's full-screen? maybe because of some of the flags? we've got no idea but the problem is here.

                    J 1 Reply Last reply 24 Aug 2021, 08:32
                    0
                    • V Offline
                      V Offline
                      Vega4
                      wrote on 24 Aug 2021, 08:28 last edited by
                      #10

                      That's our implementation of a "key-eater" event filter. No matter where we plug it (Application, main-Window) no luck...

                      CKeyEater::CKeyEater(QObject *parent) : QObject(parent)
                      {
                      
                      }
                      
                      bool CKeyEater::eventFilter(QObject *obj, QEvent *event)
                      {
                          if (event->type() == QEvent::KeyPress) {
                              QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                      
                               if(keyEvent->key() == Qt::Key_Back)
                               {
                                  qDebug("Ate a back-button press.", keyEvent->key());
                      
                                  CUIActions * ui = CUIActions::getInstance();
                                  if(ui)
                                  {
                                      if(ui->areAnyDialogsOpen())
                                      {
                                          ui->closeAllDialogsRequest();
                                      }
                                      else
                                      {
                                         exit(0);
                                      }
                      
                                  }
                      
                               }
                              return true;
                          } else {
                              // standard event processing
                              return QObject::eventFilter(obj, event);
                          }
                      }
                      
                      V 1 Reply Last reply 24 Aug 2021, 08:29
                      0
                      • V Vega4
                        24 Aug 2021, 08:28

                        That's our implementation of a "key-eater" event filter. No matter where we plug it (Application, main-Window) no luck...

                        CKeyEater::CKeyEater(QObject *parent) : QObject(parent)
                        {
                        
                        }
                        
                        bool CKeyEater::eventFilter(QObject *obj, QEvent *event)
                        {
                            if (event->type() == QEvent::KeyPress) {
                                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                        
                                 if(keyEvent->key() == Qt::Key_Back)
                                 {
                                    qDebug("Ate a back-button press.", keyEvent->key());
                        
                                    CUIActions * ui = CUIActions::getInstance();
                                    if(ui)
                                    {
                                        if(ui->areAnyDialogsOpen())
                                        {
                                            ui->closeAllDialogsRequest();
                                        }
                                        else
                                        {
                                           exit(0);
                                        }
                        
                                    }
                        
                                 }
                                return true;
                            } else {
                                // standard event processing
                                return QObject::eventFilter(obj, event);
                            }
                        }
                        
                        V Offline
                        V Offline
                        Vega4
                        wrote on 24 Aug 2021, 08:29 last edited by
                        #11

                        @Vega4
                        more.. the event filter does not get fired for any of the two other android bottom-menu keys.

                        V 1 Reply Last reply 24 Aug 2021, 08:31
                        0
                        • V Vega4
                          24 Aug 2021, 08:29

                          @Vega4
                          more.. the event filter does not get fired for any of the two other android bottom-menu keys.

                          V Offline
                          V Offline
                          Vega4
                          wrote on 24 Aug 2021, 08:31 last edited by
                          #12

                          @Vega4
                          (..) but notice... the event filter works... when a QDialog gets opened. Events are thus THEN being processed by QApplicaiton.. otherwise no luck. no idea how it's related. Smells like something due to the app being full-screen or the flags which we've set... but there's nothing that would indicate that it shouldn't work.

                          1 Reply Last reply
                          0
                          • V Vega4
                            24 Aug 2021, 08:22

                            @J-Hilk
                            Seems like we want the application to actually close. Like we want the opposite. Minimize or anything or to have control over the process but like described above. for anything we do we are unable to intercept the key_back event.

                            That's what we want - to intercept the key-back event since we need to do some cleaning up before sending the app to the back.

                            There must be some reason for which the app is completely unaware of the back-keyt presses which is why.. it is not minimized in the first place (which should be by default right?) maybe because it's full-screen? maybe because of some of the flags? we've got no idea but the problem is here.

                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 24 Aug 2021, 08:32 last edited by J.Hilk
                            #13

                            @Vega4 said in Trouble with back-button an android:

                            @J-Hilk
                            Seems like we want the application to actually close. Like we want the opposite. Minimize or anything or to have control over the process but like described above. for anything we do we are unable to intercept the key_back event.

                            That's what we want - to intercept the key-back event since we need to do some cleaning up before sending the app to the back.

                            There must be some reason for which the app is completely unaware of the back-keyt presses which is why.. it is not minimized in the first place (which should be by default right?) maybe because it's full-screen? maybe because of some of the flags? we've got no idea but the problem is here.

                            now I get it :D

                            maybe because it's full-screen? maybe because of some of the flags

                            very possible, yes! You could reduce the flags/remove them and add one after the other to see if the event comes through?

                            Have you checked Shortcuts ? From my experience, they are triggered reliably

                            Shortcut {
                                    sequence: StandardKey.Back
                                    onActivated: console.log("back Key")
                                }
                            Shortcut {
                                    sequence: StandardKey.Quitt
                                    onActivated: console.log("Quit Key")
                                }
                            Shortcut {
                                    sequence: StandardKey.Close
                                    onActivated: console.log("Close Key")
                                }
                            

                            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.

                            V 2 Replies Last reply 24 Aug 2021, 08:34
                            0
                            • J J.Hilk
                              24 Aug 2021, 08:32

                              @Vega4 said in Trouble with back-button an android:

                              @J-Hilk
                              Seems like we want the application to actually close. Like we want the opposite. Minimize or anything or to have control over the process but like described above. for anything we do we are unable to intercept the key_back event.

                              That's what we want - to intercept the key-back event since we need to do some cleaning up before sending the app to the back.

                              There must be some reason for which the app is completely unaware of the back-keyt presses which is why.. it is not minimized in the first place (which should be by default right?) maybe because it's full-screen? maybe because of some of the flags? we've got no idea but the problem is here.

                              now I get it :D

                              maybe because it's full-screen? maybe because of some of the flags

                              very possible, yes! You could reduce the flags/remove them and add one after the other to see if the event comes through?

                              Have you checked Shortcuts ? From my experience, they are triggered reliably

                              Shortcut {
                                      sequence: StandardKey.Back
                                      onActivated: console.log("back Key")
                                  }
                              Shortcut {
                                      sequence: StandardKey.Quitt
                                      onActivated: console.log("Quit Key")
                                  }
                              Shortcut {
                                      sequence: StandardKey.Close
                                      onActivated: console.log("Close Key")
                                  }
                              
                              V Offline
                              V Offline
                              Vega4
                              wrote on 24 Aug 2021, 08:34 last edited by
                              #14

                              @J-Hilk said in Trouble with back-button an android:

                              very possible, yes! You could reduce the flags/remove them and add one after the other to see if the event comes through?

                              actually I'm right at it this very moment;d it's cooking...

                              1 Reply Last reply
                              0
                              • J J.Hilk
                                24 Aug 2021, 08:32

                                @Vega4 said in Trouble with back-button an android:

                                @J-Hilk
                                Seems like we want the application to actually close. Like we want the opposite. Minimize or anything or to have control over the process but like described above. for anything we do we are unable to intercept the key_back event.

                                That's what we want - to intercept the key-back event since we need to do some cleaning up before sending the app to the back.

                                There must be some reason for which the app is completely unaware of the back-keyt presses which is why.. it is not minimized in the first place (which should be by default right?) maybe because it's full-screen? maybe because of some of the flags? we've got no idea but the problem is here.

                                now I get it :D

                                maybe because it's full-screen? maybe because of some of the flags

                                very possible, yes! You could reduce the flags/remove them and add one after the other to see if the event comes through?

                                Have you checked Shortcuts ? From my experience, they are triggered reliably

                                Shortcut {
                                        sequence: StandardKey.Back
                                        onActivated: console.log("back Key")
                                    }
                                Shortcut {
                                        sequence: StandardKey.Quitt
                                        onActivated: console.log("Quit Key")
                                    }
                                Shortcut {
                                        sequence: StandardKey.Close
                                        onActivated: console.log("Close Key")
                                    }
                                
                                V Offline
                                V Offline
                                Vega4
                                wrote on 24 Aug 2021, 08:45 last edited by Vega4
                                #15

                                @J-Hilk said in Trouble with back-button an android:

                                very possible, yes! You could reduce the flags/remove them and add one after the other to see if the event comes through?

                                It's confirmed.. the flags. Now how about it, we like need them :-)

                                J 1 Reply Last reply 24 Aug 2021, 08:56
                                1
                                • V Offline
                                  V Offline
                                  Vega4
                                  wrote on 24 Aug 2021, 08:53 last edited by
                                  #16

                                  At this very moment focusing in on the specific culprit flag which prevents the key-events from being dispatched.

                                  1 Reply Last reply
                                  0
                                  • V Vega4
                                    24 Aug 2021, 08:45

                                    @J-Hilk said in Trouble with back-button an android:

                                    very possible, yes! You could reduce the flags/remove them and add one after the other to see if the event comes through?

                                    It's confirmed.. the flags. Now how about it, we like need them :-)

                                    J Offline
                                    J Offline
                                    J.Hilk
                                    Moderators
                                    wrote on 24 Aug 2021, 08:56 last edited by
                                    #17

                                    @Vega4 I'm out of my depth here, especially as that is probably also all dependent on the window manager you/your OS uses :(


                                    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.

                                    V 1 Reply Last reply 24 Aug 2021, 09:39
                                    0
                                    • V Offline
                                      V Offline
                                      Vega4
                                      wrote on 24 Aug 2021, 08:57 last edited by
                                      #18

                                      Happens on all recent Samsung/ HTC/Huawei devices we've got at hand :-)

                                      1 Reply Last reply
                                      0
                                      • J J.Hilk
                                        24 Aug 2021, 08:56

                                        @Vega4 I'm out of my depth here, especially as that is probably also all dependent on the window manager you/your OS uses :(

                                        V Offline
                                        V Offline
                                        Vega4
                                        wrote on 24 Aug 2021, 09:39 last edited by Vega4
                                        #19

                                        @J-Hilk

                                        We've pretty much closed-in onto the problem. No key-events regarding the bottom navigation bar are dispatched and processed at all once any of the following flags are set (in conjunction or individually):

                                        • Qt.WA_TranslucentBackground
                                        • Qt.WindowTitleHint
                                        • (~Qt.WA_ContentsMarginsRespectsSafeArea)

                                        We need these flags, and it happens when ANY of the above are set.

                                        Setting visibility: Window.FullScreen doesn't seem to affect anything BUT the app looks simply ugly without the above flags. (huge empty black bars on top and bottom.. also yeah due to missing support for smart-borders on android with devices with rounded screens we had to do that by hand).

                                        Time to file a bug-report?

                                        J 1 Reply Last reply 24 Aug 2021, 09:40
                                        0
                                        • V Vega4
                                          24 Aug 2021, 09:39

                                          @J-Hilk

                                          We've pretty much closed-in onto the problem. No key-events regarding the bottom navigation bar are dispatched and processed at all once any of the following flags are set (in conjunction or individually):

                                          • Qt.WA_TranslucentBackground
                                          • Qt.WindowTitleHint
                                          • (~Qt.WA_ContentsMarginsRespectsSafeArea)

                                          We need these flags, and it happens when ANY of the above are set.

                                          Setting visibility: Window.FullScreen doesn't seem to affect anything BUT the app looks simply ugly without the above flags. (huge empty black bars on top and bottom.. also yeah due to missing support for smart-borders on android with devices with rounded screens we had to do that by hand).

                                          Time to file a bug-report?

                                          J Offline
                                          J Offline
                                          J.Hilk
                                          Moderators
                                          wrote on 24 Aug 2021, 09:40 last edited by
                                          #20

                                          @Vega4 said in Trouble with back-button an android:

                                          Time to file a bug-report?

                                          possibly,
                                          you should get at least a "good" answer ;)

                                          Don't forget to post a link here, so others can follow the report as well


                                          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.

                                          V 1 Reply Last reply 24 Aug 2021, 09:47
                                          0

                                          7/24

                                          24 Aug 2021, 08:18

                                          17 unread
                                          • Login

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