Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Timer stops/pauses if Activity is suspended in Android
QtWS25 Last Chance

Timer stops/pauses if Activity is suspended in Android

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmltimerandroidonpause
9 Posts 2 Posters 2.6k 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.
  • SyntaXS Offline
    SyntaXS Offline
    SyntaX
    wrote on last edited by
    #1

    I was wondering if I encoutered a bug in Qt / QML or this is by purpose:

    In my application I am using a Timer from QML, which assures connection with the server by sending keepalives every few seconds. But if another Activity is opened over my Main Activity (for example an AdMob Interstitial), the triggered-signals seem to don't get called anymore?

    After a little recherche I discovered that the Timer is synchronized with the animation timer, which makes sense i quess?

    But if I build the same application for iOs oder Desktop that doesn't seem to be the case?

    Is there a way to achieve a unified behavior over all platforms?
    (I tried setting the meta flag "<meta-data android:name="android.app.background_running" android:value="false"/>" to true, which seem to work, but is not recommended as I understand and comes with a lot of work on other ends)

    Another way to solve this, would be to use a QTimer in the c++ side of my code,
    but I just wanted to ask if the stated behavior is intended or if I am overlooking something here?

    best regards
    SyntaX

    KroMignonK 1 Reply Last reply
    0
    • SyntaXS SyntaX

      I was wondering if I encoutered a bug in Qt / QML or this is by purpose:

      In my application I am using a Timer from QML, which assures connection with the server by sending keepalives every few seconds. But if another Activity is opened over my Main Activity (for example an AdMob Interstitial), the triggered-signals seem to don't get called anymore?

      After a little recherche I discovered that the Timer is synchronized with the animation timer, which makes sense i quess?

      But if I build the same application for iOs oder Desktop that doesn't seem to be the case?

      Is there a way to achieve a unified behavior over all platforms?
      (I tried setting the meta flag "<meta-data android:name="android.app.background_running" android:value="false"/>" to true, which seem to work, but is not recommended as I understand and comes with a lot of work on other ends)

      Another way to solve this, would be to use a QTimer in the c++ side of my code,
      but I just wanted to ask if the stated behavior is intended or if I am overlooking something here?

      best regards
      SyntaX

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @SyntaX said in Timer stops/pauses if Activity is suspended in Android:

      But if another Activity is opened over my Main Activity (for example an AdMob Interstitial), the triggered-signals seem to don't get called anymore?

      Hello, as far as I know, on Android when an Activity goes to background, the Activity is stopped. So all Timers (even in C++) as stopped. Take a look at Android documentation
      a071510b-8f89-4829-9f2c-8943aa21e702-image.png

      So this is not a Qt/QML bug, it is the way Android is working!

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      4
      • SyntaXS Offline
        SyntaXS Offline
        SyntaX
        wrote on last edited by
        #3

        Hey @KroMignon and thanks for your reply :D

        I am aware of the Activity lifecycle, though, if I insert a Timer / runnable inside my main Activity, those triggers still get called, if another Activity is shown on Top

        code

        // test timer in Activity.onCreate
        final long startTime = 0;
        final Handler timerHandler = new Handler();
        Runnable timerRunnable = new Runnable() {
        
            @Override
            public void run() {
                long millis = System.currentTimeMillis() - startTime;
                int seconds = (int) (millis / 1000);
                int minutes = seconds / 60;
                seconds = seconds % 60;
        
                log(String.format("%d:%02d", minutes, seconds)); // this still gets called
        
                timerHandler.postDelayed(this, 500);
            }
        };
        timerHandler.postDelayed(timerRunnable, 0);
        

        Through my research I found a similar question on stackOverflow and it seems, this was a fix in Qt 5.4.

        I was just wondering, why the behaviour for Timer in QML seems to be different on iOS/desktop and Android and if it is intentional.

        KroMignonK 1 Reply Last reply
        0
        • SyntaXS SyntaX

          Hey @KroMignon and thanks for your reply :D

          I am aware of the Activity lifecycle, though, if I insert a Timer / runnable inside my main Activity, those triggers still get called, if another Activity is shown on Top

          code

          // test timer in Activity.onCreate
          final long startTime = 0;
          final Handler timerHandler = new Handler();
          Runnable timerRunnable = new Runnable() {
          
              @Override
              public void run() {
                  long millis = System.currentTimeMillis() - startTime;
                  int seconds = (int) (millis / 1000);
                  int minutes = seconds / 60;
                  seconds = seconds % 60;
          
                  log(String.format("%d:%02d", minutes, seconds)); // this still gets called
          
                  timerHandler.postDelayed(this, 500);
              }
          };
          timerHandler.postDelayed(timerRunnable, 0);
          

          Through my research I found a similar question on stackOverflow and it seems, this was a fix in Qt 5.4.

          I was just wondering, why the behaviour for Timer in QML seems to be different on iOS/desktop and Android and if it is intentional.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @SyntaX said in Timer stops/pauses if Activity is suspended in Android:

          I was just wondering, why the behaviour for Timer in QML seems to be different on iOS/desktop and Android and if it is intentional.

          You are misunderstanding something I guess. When Android put the activity into "onStop()", the activity will never gain CPU time, so no timer can work.
          On Windows/Linux/MacOS, the application will never be "frozen", so Timer will always got CPU time to execute.
          The only way to solve this with Android, is to use a Service and not an Activity.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          SyntaXS 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @SyntaX said in Timer stops/pauses if Activity is suspended in Android:

            I was just wondering, why the behaviour for Timer in QML seems to be different on iOS/desktop and Android and if it is intentional.

            You are misunderstanding something I guess. When Android put the activity into "onStop()", the activity will never gain CPU time, so no timer can work.
            On Windows/Linux/MacOS, the application will never be "frozen", so Timer will always got CPU time to execute.
            The only way to solve this with Android, is to use a Service and not an Activity.

            SyntaXS Offline
            SyntaXS Offline
            SyntaX
            wrote on last edited by
            #5

            @KroMignon said in Timer stops/pauses if Activity is suspended in Android:

            When Android put the activity into "onStop()",

            Ah sorry, what I meant was "onPause()" (that's the specific case I am dealing with) but I think this doesn't matter?
            Because the Runnable doesn't stop if I show an AdMob Interstitial on top of my main Activity, just the QML Timer stops sending events?

            best regards and thanks for your support,
            SyntaX

            KroMignonK 1 Reply Last reply
            0
            • SyntaXS SyntaX

              @KroMignon said in Timer stops/pauses if Activity is suspended in Android:

              When Android put the activity into "onStop()",

              Ah sorry, what I meant was "onPause()" (that's the specific case I am dealing with) but I think this doesn't matter?
              Because the Runnable doesn't stop if I show an AdMob Interstitial on top of my main Activity, just the QML Timer stops sending events?

              best regards and thanks for your support,
              SyntaX

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @SyntaX said in Timer stops/pauses if Activity is suspended in Android:

              Ah sorry, what I meant was "onPause()" (that's the specific case I am dealing with) but I think this doesn't matter?
              Because the Runnable doesn't stop if I show an AdMob Interstitial on top of my main Activity, just the QML Timer stops sending events?

              Hmm, I don't know what happen with QML Engine when Activity is going into "Pause" state, and I can not find anything in documentation.
              It is an interesting question.
              What about QTimer? Do they continue to work?

              Perhaps this is something you should submit to developer mailing list: https://lists.qt-project.org/listinfo/android-development

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • SyntaXS Offline
                SyntaXS Offline
                SyntaX
                wrote on last edited by
                #7

                @KroMignon said in Timer stops/pauses if Activity is suspended in Android:

                What about QTimer? Do they continue to work?

                I added a temporary QTimer in MyClass derived from Object:

                {
                    ...
                    // init in ctor:
                    QTimer *timer = new QTimer(this);
                    connect(timer, SIGNAL(timeout()), this, SLOT(debugOutput()));
                    timer->start();
                }
                
                // with the following callback:
                void MyClass::debugOutput() {
                    qDebug().noquote().nospace() << QTime::currentTime().toString() << " keepalive...";
                }
                

                After Opening an AdMob Interstitial, the Java Main Activity gets suspended (onPause gets called) and debugOutput stops printing.
                Curiously, if I close my App (push the Home button and onStop gets called) the keepalive output continues?

                Sadly I also wasn't able to find a good documentation covering the intended behaviour of Timer / Activity-lifecycle related specifications.

                I will submit to the mailing list and report back, if I gather new intel.

                Thanks again for your help,
                best regards
                SyntaX

                1 Reply Last reply
                0
                • SyntaXS Offline
                  SyntaXS Offline
                  SyntaX
                  wrote on last edited by
                  #8

                  @KroMignon Sorry for bothering you again, but I have to admit, that I never before submitted to a dev mailing list xD

                  Do I just send an Email to android-development@qt-project.org and everyone who is subscribed gets the message? Seems a bit of a overkill like @everyone to me?

                  KroMignonK 1 Reply Last reply
                  0
                  • SyntaXS SyntaX

                    @KroMignon Sorry for bothering you again, but I have to admit, that I never before submitted to a dev mailing list xD

                    Do I just send an Email to android-development@qt-project.org and everyone who is subscribed gets the message? Seems a bit of a overkill like @everyone to me?

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #9

                    @SyntaX said in Timer stops/pauses if Activity is suspended in Android:

                    Do I just send an Email to android-development@qt-project.org and everyone who is subscribed gets the message? Seems a bit of a overkill like @everyone to me?

                    No problem, you first have to register to the mailing list, and then you can send emails to it. This is the Qt-Android developers mailing list. So your mail will be received by each member.
                    That's the way mailing lists works ;-)

                    You can also try to contact BogDan Vatra from KDAB, which is the main developer of Qt/Android

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    0

                    • Login

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