onEnterSleep Called Before onLayout

In the simulator as of SDK 1.1.2, onEnterSleep is called before onLayout. This implies that onEnterSleep cannot rely on initialization performed in onLayout. Is this expected behavior?

class SandboxView extends Ui.WatchFace {
function onLayout(dc) {
Sys.println("onLayout");
}

function onShow() {
Sys.println("onShow");
}

function onEnterSleep() {
Sys.println("onEnterSleep");
}

}


Output:
onEnterSleep
onLayout
onShow
  • It may be just me, but I had some issues with "Sys.println" where the first line to be printed didn't show in the Eclipse message screen.
    It was a loop, sort of like printing "1,2,3" and having it repeat "1,2,3,1,2,3,1,2,3." What I observed was "2,3,1,2,3,1,2,3."

    So, it may be that your "onEnterSleep" is just not getting printed. But, the program IS entering the function.
  • As long as this behavior is consistent between devices and the simulator, you can assume that the device will be in low-power mode when onLayout() is called, and that you can't access anything that would be initialized by that function.

    That doesn't seem to be a big deal. All you should need to do in onEnterSleep()/onExitSleep() is set a boolean and refresh the UI.

    Travis
  • I agree with Travis that this probably isn't much of a problem--I believe we just make the assumption in the simulator that the watch face is starting in low power mode. If I create an app to test this similar to what you've posted and load it onto a device, I see initially this:

    {code}
    onLayout
    onShow
    onExitSleep
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onUpdate
    onEnterSleep
    {code}

    I didn't see any cases where onEnterSleep was called ahead of other functions on the device, so I don't think you'll encounter this issue on a device. I've created an issue to ask whether what we see in the sim is by design and to have us look at whether this can happen on the device (and how we would handle it).
  • I agree with Travis that it's not that big of a deal "as long as this behavior is consistent between devices and the simulator." However, if the calling order is different on the simulator, then it implies that certain code may behave differently on the device than it does on the simulator. Based on this, I am wondering if the change was intentional.
  • This was addressed in the 2.4.1 release:

    Discovered several bugs relating to devices behaviors with watch faces:
    • fr735 calling onShow before each onUpdate
    • vivoactive3 not refreshing the interface when Ui.requestUpdate() called from with the onEnterSleep call.
    • vivoactive3 not entering low power mode despite onEnterSleep having been called when returning from an application
    • vivoactive3 not entering low power mode immediately following onEnterSleep after leaving the widget loop.

    onEnterSleep and onExitSleep are unrelated to the view life-cycle and users shouldn't depend on the order of these calls relative to the onLayout, onShow, onUpdate functions.