What is the best place to add an initializaition of a data field at the beginning of a recording?

I want to create a SimpleDataField, which has to be initialized at the beginning of a recording.

What is the best place for the init code?

So far I understand the initialize() method is for initialization creating the object, and so this is not the right place to add init code for the beginning of a recording.

 I would appreciate an answer.

  • Yes, your derived SimpleDataField is most likely going to be initialized before the activity is started. You can use DataField.onTimerStart() to be notified when the activity starts. There are a handful of other functions if you need to know about other recording events (pause/stop/lap/...).

    One thing that you must keep in mind is that the user might start an activity, and then while the clock is running add your data field to one of the screens. The onTimerStart() method should still get called (even if the timer has been running). If you want to detect this situation, you should be able to do so by looking at Activity.Info.timerState.

    hidden var mActivityStartedBeforeDataField;
    
    function initialize() {
        SimpleDataField.initialize();
    
        var activityInfo = Activity.getActivityInfo();
        mActivityStartedBeforeDataField = activityInfo.timerState != Activity.TIMER_STATE_OFF;
    }