How do I get a data field to use data that persists when the user changes the view screen during a workout?

I'm new to developing for Garmin, so sorry if this is an obvious question.  I've written a data field that stores data from previous calls to compute() and uses this stored data to create the information shown in the data field.  This works fine when the screen the data field is on is always selected and visible to the user.  On my device (Edge 830) I swipe to a different data screen, so my custom data field is not visible and then swipe back, and the initialize method is called on my data field and I don't believe I have access to data stored when the data field was previously visible. 

I've seen references to the View object supporting onHide, so able to use an object store to persist data, but this is just a data field that is used in a standard Garmin activity - I don't want to write the whole device app.

Is it possible to achieve what I'm asking?  Thanks!

  • I've never tested with a real Edge, but as far as watches go, a Connect IQ data field is only initialized once per activity, so any internal app state should persist when the field is hidden and shown again. I always assumed that Edge devices work the same way.

    The exception is when you Resume Later, in which case CIQ data fields are closed and reinitialized when the activity resumes. In this case, you would need to persist state to storage to give the user a seamless experience, but I don't know any app that does this, not even Garmin's own Combo Running Power field.

  • Thanks for the hint - I'd put the data in my Drawable object, which was getting discarded when the data field was not visible and the initialised when the data field was visible again.  I've now moved the data into a separate class that is created by my class that extends DataField, and a reference is handed to the Drawable object in the onLayout method, so that it's available when the draw method is called.

    Seems to work now - thanks again!

  • If you calculate in compute(), save it in a variable for your class, and then update your layout in onUpdate() with that variable, you don't need any extra classes.

  • There is quite a bit of data to store and it made sense to bundle it all with the methods to draw it in a class. But yes, it did cross my mind to create a dictionary and pass all the data in one go to the drawable class. 

  • You might have issues if you want to have this DF for watches with a 28K limit for DF.  I'm not sure why you are storing anything, and it might not be needed.  Only if you want your own history of something for the next time the activity is used.