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. Save on Exit on Android
Forum Updated to NodeBB v4.3 + New Features

Save on Exit on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 3 Posters 651 Views 2 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.
  • TomZT Offline
    TomZT Offline
    TomZ
    wrote on last edited by
    #1

    In my Qt based application I save some downloads at shutdown.

    I do that by connecting to QCoreApplication::aboutToQuit and that signal handler won't return until the save has completed.
    This works just fine on my Linux desktop build.

    On Android, to my surprise, the code doesn't complete and the file is only saved partially. I triple checked that this is not due to some assert or whatever being thrown, and after I put a log line in my save-loop I noticed that every time it actually stops saving after a different number of iterations (typically less than 10 out of 30000).

    My guess is that the aboutToQuit signal is interrupted after 20 milliseconds or so and a hard kill is executed for my application.

    I there a way to get an earlier notification of shutdown where I can safely save?

    JonBJ 1 Reply Last reply
    0
    • TomZT TomZ

      In my Qt based application I save some downloads at shutdown.

      I do that by connecting to QCoreApplication::aboutToQuit and that signal handler won't return until the save has completed.
      This works just fine on my Linux desktop build.

      On Android, to my surprise, the code doesn't complete and the file is only saved partially. I triple checked that this is not due to some assert or whatever being thrown, and after I put a log line in my save-loop I noticed that every time it actually stops saving after a different number of iterations (typically less than 10 out of 30000).

      My guess is that the aboutToQuit signal is interrupted after 20 milliseconds or so and a hard kill is executed for my application.

      I there a way to get an earlier notification of shutdown where I can safely save?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @TomZ
      Googling qt android abouttoquit shows you others have problems, indeed some claim aboutToQuit() is not called at all! Various hits, one accepted solutuon at https://stackoverflow.com/a/55416311

      Use QGuiApplication::applicationStateChanged to save application's states.

      In the case of Windows, clean up the resource with: QApplication::aboutToQuit

      In the case of Android, let the system clean up the resource.

      Suggestion is that QGuiApplication::applicationStateChanged is what to use under Android?

      Mind, examples seem to only be doing "clean up" (quick?) rather than a "save file" (slow?). Might be that this still falls foul of some Android timeout...

      1 Reply Last reply
      0
      • TomZT Offline
        TomZT Offline
        TomZ
        wrote on last edited by
        #3

        @JonB said in Save on Exit on Android:

        Suggestion is that QGuiApplication::applicationStateChanged is what to use under Android

        Awesome, tested this and indeed the way to go.

        I'll add a "save" method to call on reaching mode 'inactive' which should keep the shutdown to do almost nothing.

        ekkescornerE 1 Reply Last reply
        0
        • TomZT TomZ

          @JonB said in Save on Exit on Android:

          Suggestion is that QGuiApplication::applicationStateChanged is what to use under Android

          Awesome, tested this and indeed the way to go.

          I'll add a "save" method to call on reaching mode 'inactive' which should keep the shutdown to do almost nothing.

          ekkescornerE Offline
          ekkescornerE Offline
          ekkescorner
          Qt Champions 2016
          wrote on last edited by
          #4

          @TomZ I'm doing something similar:

          // iOS: Attention: NO SIGNAL AboutToQuit
          // Android: SIGNAL if leaving the App with Android BACK Key
          // Android: NO SIGNAL if using HOME or OVERVIEW and THEN CLOSE from there
          void ApplicationUI::onAboutToQuit()
          {
              qDebug() << "On About to Q U I T Signal received";
              startCaching();
          }
          
          void ApplicationUI::onApplicationStateChanged(Qt::ApplicationState applicationState)
          {
              qDebug() << "S T A T E changed into: " << applicationState;
              // Hint: suspend not from macOS - only Android, iOS
              if(applicationState == Qt::ApplicationState::ApplicationSuspended) {
                  startCaching();
              }
          }
          

          FYI: I'm storing my app data as JSON files. Can be some MB.
          Doing caching not only if closing the app, but also if moving to background. works well for my customers.
          (And if app is running in KIOSK mode on Android, I'm caching in intervals using a Timer)

          ekke ... Qt Champion 2016 | 2024 ... mobile business apps
          5.15 --> 6.9 https://t1p.de/ekkeChecklist
          QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

          1 Reply Last reply
          1

          • Login

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