Values on resumed activity

I'm creating datafield with 2 FitContributors: first MESG_TYPE_SESSION, second MESG_TYPE_LAP.

I'm calling setData method for first field in onTimerPause() and onTimerStop() methods.
For second field in onTimerLap().
I'm creating fields in initialize() method.

I have problem with resuming activity.
When user choses resume later activity my values resets to 0.

Possible workaround for first field is to use Toyboy::Application:Storage and save value in onTimerPause()/onTimerStop(). Then read it in initialize(). I think this wouldn't work with second field.
How to solve this problem?

Source code is avaiable on github: https://github.com/tymmej/HikeField/blob/master/source/HikeField.mc
  • Former Member
    Former Member over 7 years ago
    If the user selects "resume" later, the Connect IQ data fields will be shut down. If you need to persist any data for an activity when your field is terminated prior to the activity completing, you can do so. A good place to do this may be in the AppBase.onStop() method. Saved values can be reloaded when the application is started back up. This could be done in AppBase.onStart() or in an initializer.
  • I was thinking about this problem and that's my thougths:
    1) in function onTimerStop() I'll save to storage:
    a) startTime from Activity.Info
    b) total steps
    c) array with steps per lap
    I'll also save b and c to FitContributor.
    2) in onTimerStart() I'll read data from storage
    If startTime from storage and current activity are different: that's new activity and I set variables to 0's.
    If both variables are same: that's same activity and I use values from storage.
    3) in onTimerReset() I can clear persistent storage (but don't have to).

    In order to "Resume later" user has to stop activity which will invoke onTimerStop(), so I don't have to use AppBase.onStop().
    onTimerPause() and onTimerResume() are irrelevant to problem.

    Did I missed anything?
  • This is a case where it can help to put together something really simple to see how things happen. Here's one I did that I can run on a watch (an f5 in this case) and log things. Like Brian said AppBase.onStop is the logical place to save things off, but it has to be smart enough to know if anything should be saved. If you do manual pause/resume you'll see more timer start/stops than in the log file I also attached. It gets more complex with manual and auto pause. (more timer events for start/stop/pause/resume)

    //first file
    using Toybox.Application;
    using Toybox.System as Sys;

    class dfstateApp extends Application.AppBase {

    function initialize() {
    AppBase.initialize();
    }

    // onStart() is called on application start up
    function onStart(state) {
    Sys.println("onStart");
    }

    // onStop() is called when your application is exiting
    function onStop(state) {
    Sys.println("onStop");
    }

    // Return the initial view of your application here
    function getInitialView() {
    return [ new dfstateView() ];
    }

    }

    --------------------------------------------------------------------------------
    //second file
    using Toybox.WatchUi;
    using Toybox.System as Sys;

    class dfstateView extends WatchUi.SimpleDataField {
    var state="??";

    // Set the label of the data field here.
    function initialize() {
    SimpleDataField.initialize();
    label = "timer state";
    }

    // The given info object contains all the current workout
    // information. Calculate a value and return it in this method.
    // Note that compute() and onUpdate() are asynchronous, and there is no
    // guarantee that compute() will be called before onUpdate().
    function compute(info) {
    // See Activity.Info in the documentation for available information.
    return state;
    }

    function onTimerLap() {
    state="Timer Lap";
    Sys.println(state);
    }

    function onTimerPause() {
    state="Timer Pause";
    Sys.println(state);
    }

    function onTimerReset() {
    state="Timer Reset";
    Sys.println(state);
    }

    function onTimerResume() {
    state="Timer Resume";
    Sys.println(state);
    }

    function onTimerStart() {
    state="Timer Start";
    Sys.println(state);
    }

    function onTimerStop() {
    state="Timer Stop";
    Sys.println(state);
    }

    }
    --------------------------------------------------------------------------------------------
    onStart <new activity
    Timer Start
    Timer Stop
    onStop <recording discarded here
    onStart
    onStop <back to watchface

    onStart <new activity
    Timer Start
    Timer Stop <resume later done here
    onStop
    onStart
    Timer Start
    Timer Stop
    onStop <recording saved here


    Try something simple like this to work out when/if to save anything, and when/if to use the saved data. I'm just showing the onTimer calls, so you'll want more logic based on the data itself I imagine.
  • Former Member
    Former Member over 7 years ago
    I was thinking about this problem and that's my thougths:
    1) in function onTimerStop() I'll save to storage:
    a) startTime from Activity.Info
    b) total steps
    c) array with steps per lap
    I'll also save b and c to FitContributor.
    2) in onTimerStart() I'll read data from storage
    If startTime from storage and current activity are different: that's new activity and I set variables to 0's.
    If both variables are same: that's same activity and I use values from storage.
    3) in onTimerReset() I can clear persistent storage (but don't have to).

    In order to "Resume later" user has to stop activity which will invoke onTimerStop(), so I don't have to use AppBase.onStop().
    onTimerPause() and onTimerResume() are irrelevant to problem.

    Did I missed anything?


    This seems reasonable to me, but you probably know better than I do how the device behaves when suspending an activity.