onUpdate Before Hide

I'd need an experiment to prove it, but it seems like the OS will call onUpdate just before onHide when a watch face is shutting down. I'd like a way to discern between an update when the app is shutting down and a "normal" update so I can avoid expensive drawing operations as the app is shutting down. This would help make the exit animations smoother. Has anyone else encountered this and come up with a solution? Thanks.
  • Former Member
    Former Member over 10 years ago
    Any situations like this that I want to specifically control I use a flag. So you might do something like..

    function onUpdate(dc) {

    if(isClosing){
    return;
    }

    // normal code


    }
  • Any situations like this that I want to specifically control I use a flag. So you might do something like..

    function onUpdate(dc) {

    if(isClosing){
    return;
    }

    // normal code


    }


    Sounds good, but how do you know when to set isClosing?
  • Former Member
    Former Member over 10 years ago
    I overlooked
    .... when a watch face is shutting down..

    and read
    ... when the app is shutting down.....

    so I figured it would be an expected event. Are you referring to when when the watch face enters low power mode? because you wouldn't have to worry, because the user wouldn't be looking.
  • Here's some example code to illustrate. I'd like to avoid any expensive operation when the user swipes or presses a button to exit, but setting the variable "hiding" in onHide() doesn't accomplish this due to the order in which the functions are called. Specifically, it seems that we get final onUpdate() calls prior to the onHide() call. This makes it so the app exit isn't as snappy as it could otherwise be.

    As a sidenote, this code won't run on the simulator because the simulated WDT doesn't handle the Sys.getTimer() spin loop realistically.

    function onUpdate(dc) {
    var stringDim = dc.getTextDimensions("A", Gfx.FONT_SMALL);
    var drawStartMillis = Sys.getTimer();

    if(hiding) {
    return;
    }

    // Simulate expensive operation (exaggerated for illustration)
    while(Sys.getTimer() - drawStartMillis < 1500) {
    }

    dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
    dc.drawText(10,
    10 + stringDim[1] * (testIndex % 6),
    Gfx.FONT_SMALL,
    Lang.format("$1$: hiding=$2$", [testIndex, hiding]),
    Gfx.TEXT_JUSTIFY_LEFT);

    testIndex++;
    }
  • Former Member
    Former Member over 10 years ago
    Firstly, are we talking an app or a watch face? ...or a widget or data field?
    When I close a watch face in the simulator it closes as such
    View onHide
    App onStop
    Complete
    App onStop

    And that included a println in onUpdate, which wasn't called after I used the simulator File > Kill app
  • I ran the above code on hardware in a watch face. We must be getting new onUpdate() calls driven by the exit button press just before onHide(), because the exit can be slow in the example code even after slow updates begin. That is, if onHide() happened first, then I believe we would take the return branch and the exit would be fast.
  • Former Member
    Former Member over 10 years ago
    And you're calling onHide in the extended Ui.WatchFace class correct?
  • Correct. So to summarize, I think what is happening is:

    • onUpdate() hasn't happened for a while due to low power
    • User presses button to exit watch face
    • onUpdate() fires, causing us to incur the expensive operation
    • onHide() fires (too late to help)
  • What are you doing in onUpdate that's so expensive? Under normal conditions, onUpdate is called either every minute (in low power) or every second (not in low power)?
  • An inverse orthographic projection of the earth, split into multiple calculations of several hundred milliseconds each. (See https://apps.garmin.com/en-US/apps/6b00ea27-f789-44ea-9f4f-f0c118bc3abe.)