I want to update some elements in my app's MainActivity every 30 seconds. I need that to happen only when MainActivity has focus, so once I change activities or close the app, this periodic task should stop.
I have tried using a Handler with Runnable and recursive postDelayed but I keep having issues with runnables not stopping, multiple ones running at the same time, etc.
I'm not even sure if this is the way I'm supposed to use for that wanted functionality, so here I am asking if anyone has done something similar and can share his way of achieving this.
PS. I'm writing my app in Java.