Detect when a datafield exit from foreground

Former Member
Former Member
Hi, I've a datafield that works with an ant device. Beacause resuming from sleep delete the ant configuration, I've added in the onUpdate a check that reinitialize the ant channel if the last message is more than 2 seconds old. The problem is that if I open my datafield, then I go back to the home screen, after a few seconds the GArmin (not the app, the full device) freeze itself. This appens only when there are no any message incoming and I think it's because I try to reinitilize the ant channel while not in foreground.

So my question is: how can I detect when the app loose the foreground (onHide() doesn't work ) so that I can block the reinitialization of the Ant channel until back to foreground (the onShow() seems to work properly)?

I'm using the SDK 2.4.x

Thanks!
  • I'd structure the DF differently. Things like onLayout()/onUpdate(), I only use to display the data. (onUpdate runs every second when the DF is visible, and will run multiple times if you have the same DF more than once on the same screen.

    I do the "heavy lifting" in compute(). It runs every second and only once, if the DF is being displayed or not, and in the case where the DF might be on the same screen more than once. (and it's only run once if you have the same DF on more than one screen).

    Take a look at the MoxyField sample in the SDK.
  • Former Member
    Former Member
    Thanks, I moved the "last message check" in the compute() as you suggested, but I still have the problem that if the DF goes out of the foreground the garmin freezes after a while... Is there any limitation working with ant channel while not in foreground (aka close, release and reopen the channel)?
  • Have you looked at the MoxyField sample in the SDK? It's doing something like you are trying to do with a sensor. Look at the overall structure of how it works.
  • Former Member
    Former Member
    Yes I did, I'm trying to figure out the differences with my project... Debugging is very difficult... I can't log the problem to understand why it freezes... :(
  • I can't see your code so some of this is speculation but the ANT code managing the connection of data page responses should not happen in the view level of the application. These should all be happening outside of onUpdate/onCompute/etc. Are you starting the ANT connection in onStart or somewhere else. Another thing of note is that the data field continues to run even if it isn't the actively shown screen, you shouldn't need to re-initialize the ANT channel if the activity is still running but the user has simply changed to another screen and then back.
  • Former Member
    Former Member
    I can't see your code so some of this is speculation but the ANT code managing the connection of data page responses should not happen in the view level of the application. These should all be happening outside of onUpdate/onCompute/etc. Are you starting the ANT connection in onStart or somewhere else. Another thing of note is that the data field continues to run even if it isn't the actively shown screen, you shouldn't need to re-initialize the ANT channel if the activity is still running but the user has simply changed to another screen and then back.


    The point is that, if the device enters the sleep mode, it resets the ant channel configuration so the DF can't communicate with the device after resuming from sleep... The only solution I found was to detect this state by checking the time from the last message. The onUpdate/onCompute run every second even without communication so I can use them to check the time passed from the last message.

    Do you know a better solution?
  • Not sure what you mean by sleep mode. The ANT channels definitely stay open even while not being displayed on the current page. Further, you shouldn't need to do anything in compute directly with the channel as all the new data processing happens in GenericChannel.onMessage(). Although I have seen cases where the Ant channel has problems receiving data, despite being successfully opened (I've seen this with both the Asset Tracker Ant+ channel from a Garmin Alpha as well as from private ANT messages from RunScribe). In these cases, it helps to have a timer going that gets checked in the compute method and close/re-opens the channel if you haven't gotten data messages for a certain amount of time (I like 15 to 30 seconds).

    So what you would really want is a notification for when the device comes out of "sleep mode". The timer trick above might also help solve your problem.

    Jim, is that true that if you have the same field open multiple times, they effectively share the same object so compute only gets called once but onUpdate gets called for each? I don't think I've tried it but that is very interesting.
  • Yup. If you have the DF twice on the same screen, each second you'll see two calls to onUpdate(), and one call to compute(). Easy to show with a couple println calls in the code and a .txt log file. You can also see it in the sim.
  • Former Member
    Former Member
    Hey Davide,

    Would you be willing to send in a code sample that reproduces this case? We'd like to try and reproduce and analyze it. You can send source to [email][email protected][/email]

    Thanks,
    -Coleman
  • Former Member
    Former Member
    Not sure what you mean by sleep mode. The ANT channels definitely stay open even while not being displayed on the current page. Further, you shouldn't need to do anything in compute directly with the channel as all the new data processing happens in GenericChannel.onMessage(). Although I have seen cases where the Ant channel has problems receiving data, despite being successfully opened (I've seen this with both the Asset Tracker Ant+ channel from a Garmin Alpha as well as from private ANT messages from RunScribe). In these cases, it helps to have a timer going that gets checked in the compute method and close/re-opens the channel if you haven't gotten data messages for a certain amount of time (I like 15 to 30 seconds).

    So what you would really want is a notification for when the device comes out of "sleep mode". The timer trick above might also help solve your problem.

    Jim, is that true that if you have the same field open multiple times, they effectively share the same object so compute only gets called once but onUpdate gets called for each? I don't think I've tried it but that is very interesting.



    When you press the on/off button it show you the options "sleep/power off". If you choose sleep and wait for a moment, when you exit from sleep the device will not receive any message and you need to reinitialize the ant channel. I found the solution with timestamps (that I've adapted), reading this post https://forums.garmin.com/forum/developers/connect-iq/128379-