How can you preserve global variable data , global array data when you exit the watchface

How can you preserve global variable data , global array data when you exit the watchface, ie go to a widget, an alarm triggers .

When I return to watchface, does it always re-intialze the watchface? I want to still keep the variable values and use them when 
returning to the watchface.

Could someone please explain what happens when you exit a watchface, then return to the watchface, especially with the variables.

Thanks.

  • You put things in the ObectStore/Application storage when you exit and read that back in when the WF starts.  See the programmer's guide and the OjectStore sample in the SDK.

  • Apps of type watchface are typically terminated when the user navigates away from the watch face. This isn't absolutely written in stone, but that is how it has worked for all devices I can recall.

    With all ConnectIQ application types:

    • Application.onStart() is called at app startup
    • Application.onStop() is called at app shutdown

    If either of these are called, you know the state of the application and what is going to happen next. These are ideal hooks to use to write code to read/write application state.

  • One thing to keep in mind here is if the app (in this case a WF) has a background process, onStart() and onStop() are both called each time the background runs.

    While it's safe to always read the saved values in onStart(), in onStop() you'll want to have a bit of smarts so it only saves the data if it's called for the main app and not the background.  An error will be thrown if you try to save the data as the background process.

  • How is it possible that on "function onStop(state)" i can load and save numbers but not arrays? 
    Both lastTime and emptyArrays are declared after the class definition 

    class TestView extends WatchUi.WatchFace {
    var lastTime, emptyArray;
    and 
    class TestApp extends Application.AppBase {
    var lastTime, emptyArray;
    So in the code it loads the first but not the second...

    lastTime = Application.Storage.getValue("myLastTime");
    Storage.setValue("myLastTime", lastTime);
    System.println ("SAVED lastTime " + lastTime); //-> correct value evaluated in TestView.mc

     emptyArray = Application.Storage.getValue("myEmptyArray");
     Storage.setValue("myEmptyArray", emptyArray);
     System.println ("SAVED emptyArray " + emptyArray);  //-> null (but printed in TestView.mc at last line correctly...)
    it gives Error: Symbol Not Found Error Details: Could not find symbol 'emptyArray'
  • A couple things to start,  You have var lastTime, emptyArray in both your AppBase class and your View class.  They are local to those classes and if you change them in the View, the AppBase remains unchanged.  A quick way to handle this is have the var.. outside both classes, and that way those are global to your app (take the var statements out of both classes).

    The other way, is in getInitalView, save off the view, and then in AppBase, reference them as "view.lastTime" and "view.emptyArray", with the var only done in your view class.  You'll want to check "view" to make sure it's not null. (getInitialView hasn't run yet)

    Then the way you are declaring these, they start out as null, and not an array in the case of emptyArray, so when you do the getValue, you'll see null and not an empty array.  lastTime will also be null.

    Also, if you plan on using a background servive, onStop() will be called each time the background runs, and not just when the main app exits, so you want to be a bit careful there.

  • thank you very much!!! I some how managed to accomplish the job! I was over writing the variables, that's why it was not doing what I wanted.

    OFFTOPIC.
    is there a way to define a palette dinamically as an input to drawBitmap? As an attribute to image maybe? image. ... ?

        image = WatchUi.loadResource( Rez.Drawables.id_test );
        dc.drawBitmap(100, 45, image);

    having image.xlm

    <drawable-list id="image" background="Graphics.COLOR_TRANSPARENT">
        <bitmap id="id_ms" filename="image/test.png">
            <palette disableTransparency="false">
                <color>FF0000</color>
                etc...
            </palette>
         </bitmap>
    </drawable-list>
  • OFFTOPIC.
    is there a way to define a palette dinamically as an input to drawBitmap? As an attribute to image maybe? image. ... ?

    The pallet and what you set in the xml is used when the bitmap is compiled and can't be changed at runtime

  • So there is a way to define dynamically this command Rez.Drawables."ID" ?

        var color= "Yellow";
        var ID= "id_test_+color; // so i have the sting (i don't know it then it is possible to use it)
        System.println (ID);
        if (foregroundColor == 16755200) {
            image = WatchUi.loadResource( Rez.Drawables."ID" );
        }

    so I can create a xlm file with the same image and diffent palettes and recall the one I need from the View.