UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Component.onDestruction not called when closing app (QML with C++ plugin)

    Scheduled Pinned Locked Moved Solved App Development
    11 Posts 5 Posters 1.9k Views 2 Watching
    Loading More Posts
    • 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.
      • lduboeufL Offline
        lduboeuf @Aloys
        last edited by lduboeuf

        @aloys, yes confirmed. You can work around with the Qt.application.active signal.
        Example here: https://github.com/ubports/dialer-app/blob/xenial/src/qml/dialer-app.qml#L32

        And the signal handler: https://github.com/ubports/dialer-app/blob/xenial/src/qml/dialer-app.qml#L65

        A 1 Reply Last reply Reply Quote 1
        • mardyM Offline
          mardy
          last edited by

          Please note that the Qt.application.active signal is also emitted when the application simply goes to the foreground. If you need to save some data, it's indeed a good idea to do it at that time, since you don't know whether the application will later be killed.

          But if you really need to know when the application is being closed, then Qt.application has an aboutToQuit() signal which you can listen to via a Connections element.

          1 Reply Last reply Reply Quote 2
          • A Offline
            Aloys @lduboeuf
            last edited by Aloys

            Thank you @lduboeuf and @mardy , I use the Qt.application.active signal to fix my issue (here). What I need is to save data, so this method is appropriate for my app.

            Note that when launching app on desktop (with clickable desktop), closing the app with the cross button (at upper right) will not generate this signal. But it seems not a problem at all on the device because to close the app, user need to "slide and throw" which cause a switch in an inactive state.

            This should allow me to release new version of my Taquin app!

            K 1 Reply Last reply Reply Quote 0
            • K Offline
              kugiigi @Aloys
              last edited by

              @aloys Maybe you should also call your function in aboutToQuit so that you're sure it'll be triggered. Take note that an app can be closed in different ways on Lomiri - from app spread, from launcher, from close button when in windowed mode, from crashing, from out of memory.

              A 1 Reply Last reply Reply Quote 1
              • A Offline
                Aloys @kugiigi
                last edited by Aloys

                Thanks @kugiigi , you're right, I will do it, this will be more robust.

                And that's so simple! it works by just adding:

                Connections {
                        target: Qt.application
                        onAboutToQuit: {
                            console.log("About to Quit")
                        }
                    }
                

                But finding the solution is not simple!

                A 1 Reply Last reply Reply Quote 0
                • A Offline
                  Aloys @Aloys
                  last edited by

                  In fact, not so simple! I just discover another unexpected behavior.
                  I use Settings to save status of the game, and when adding saving on signal AboutToQuit(), the file (taquin.aloysliska.conf) is not updated.

                  See below a simplified example from my app:

                      Settings { 
                          id: settings
                          property int savedmovcount: 0
                      }
                  
                  ...
                  
                  Connections {
                          target: Qt.application
                          onAboutToQuit: {
                              settings.savedmovcount = movcount     // save movcount in settings
                              console.log("savedmovcount", settings.savedmovcount)   // re-read and display saved value
                          }
                      }
                  

                  I see in the log that settings.savedmovcount has the expected value, but the file was not updated.

                  CiberSheepC mardyM 2 Replies Last reply Reply Quote 0
                  • CiberSheepC Offline
                    CiberSheep @Aloys
                    last edited by

                    @aloys You could save the var in Settings when it changes

                    Another planet, another time, another universe!

                    A 1 Reply Last reply Reply Quote 0
                    • A Offline
                      Aloys @CiberSheep
                      last edited by

                      @cibersheep I will probably do as you said in a next release (I release recently with saving only on application inactive).
                      I did not do it it initially because it seems for me more proper to not cause file writing on each tile move done by the user. When playing, we can easily have hundreds of move, so hundred time of file writing (in fact i do not know how the os manage this, with cache or direct flash writing?...).

                      Considering the problems I have encountered, this would be much simpler ... if i do not find another unexpected behavior!!! I was a bit discouraged with all these problems and the time spent on them, but let see the positive point, I have learned a lot.

                      Thank you all for your help!

                      1 Reply Last reply Reply Quote 0
                      • mardyM Offline
                        mardy @Aloys
                        last edited by

                        @aloys said in Component.onDestruction not called when closing app (QML with C++ plugin):

                        In fact, not so simple! I just discover another unexpected behavior.
                        I use Settings to save status of the game, and when adding saving on signal AboutToQuit(), the file (taquin.aloysliska.conf) is not updated.

                        Try also calling settings.sync(), that should help.

                        A 1 Reply Last reply Reply Quote 1
                        • A Offline
                          Aloys @mardy
                          last edited by Aloys

                          @mardy I did not success to make it work, I have error:

                          qrc:/Main.qml:85: TypeError: Property 'sync' of object QQmlSettings_QML_16(0x13ee5d0) is not a function
                          

                          I may miss something! I have id of Settings named appSettings, then I call appSettings.sync().

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post