Save parameters onClose and retrieve them onStart

Former Member
Former Member
Hello,

I have build an Application for the Fenix 3 and I have some parameters that I will the values when the application exit and retrieve then when the application start.

I have found the AppBase.getProperty(key) or AppBase.setProperty(key) but I am a new programmer and a good example to how to save a parameter and then get it back
will be for me very helpful.


I have the intern variable Dist for the distance.

There is my code

class FRAC2App extends App.AppBase {

//! onStart() is called on application start up
function onStart() {

App.getApp().getProperty(0, Dist);

sys.println("start");
}

//! onStop() is called when your application is exiting
function onStop() {
App.getApp().setProperty(0, Dist);

sys.println("stop");

}


Kind regards
  • class FRAC2App extends App.AppBase {

    var Dist;

    //! onStart() is called on application start up
    function onStart() {
    Dist = App.getApp().getProperty(0);
    }

    //! onStop() is called when your application is exiting
    function onStop() {
    App.getApp().setProperty(0, Dist);
    }

    //...
    }


    This is covered in the ObjectStore example. It shows how to use the functions with both string and enumerator keys. I wouldn't use an integer constant for the property key (it becomes very difficult to keep track of them).
  • As Travis said the ObjectStore sample is a good place to look, and with the 1.2.0 Sdk, the same sample also shows how to use "user Settings", where the user can set values for your app.
  • Former Member
    Former Member over 9 years ago
    Before you retreive the properties, you have to load them. Furthermore, you have the parameters of getProperty(key) wrong.
    Since your code is in your app class, you don't have to use App.getApp().

    To load the properties, use

    loadProperties();
    Dist = getProperty("distance");


    To set the property, use:

    setProperty("distance", Dist);
  • Also, when you do the "getProperties()", if the key doesn't exist, a null will be returned. For example, the first time your app is run, it could be null if you don't have it defined as a property in your resources

    with with TeunMo's code above:

    Dist = getProperty("distance");

    You'd want to add something like this to check for a null:

    if(Dist==null) {
    Dist=0; //start at 0 if no known distance
    }
  • Before you retreive the properties, you have to load them.

    You should not have to do this. It was necessary at a time in the past, but it is not necessary now.

    Furthermore, you have the parameters of getProperty(key) wrong.

    Not necessarily. If 0 is the key that he wants, and Dist is the value he wants to store, the code was correct. As I mentioned above, and you are showing above as well, using named keys is definitely easier to read.

    Since your code is in your app class, you don't have to use App.getApp().

    Yes. Provided the code is inside the derived AppBase (as is always the case in onStart() and onStop()), you don't need to access it via the singleton accessor.
  • Former Member
    Former Member over 9 years ago
    It seems to works

    Hi,

    Thank for your help

    This code is working.

    Maybe not very beautiful, but works

    enum
    {
    Dist_Store,
    Time_Store,
    VMA_Store,
    Percent_Store,
    TM_Store
    }


    function onStart() {

    Dist = App.getApp().getProperty(Dist_Store);
    Time = App.getApp().getProperty(Time_Store);
    VMA = App.getApp().getProperty(VMA_Store);
    Percent = App.getApp().getProperty(Percent_Store);
    TM = App.getApp().getProperty(TM_Store);
    }
    function onStop() {
    App.getApp().setProperty(Dist_Store, Dist);
    App.getApp().setProperty(Time_Store, Time);
    App.getApp().setProperty(VMA_Store, VMA);
    App.getApp().setProperty(Percent_Store, Percent);
    App.getApp().setProperty(TM_Store, TM);
    }