44
55class Activity :
66
7+ throttle_async_call_counter = 0
8+
79def __init__ (self ):
810self .intent = None # Store the intent that launched this activity
911self .result = None
@@ -14,12 +16,14 @@ def onCreate(self):
1416pass
1517def onStart (self ,screen ):
1618pass
17- def onResume (self ,screen ):# app gets foreground
19+
20+ def onResume (self ,screen ):# app goes to foreground
1821self ._has_foreground = True
19- mpos .ui .th .add_event_cb (self .thc , 2 )
22+ mpos .ui .th .add_event_cb (self .task_handler_callback , 1 )
2023
2124def onPause (self ,screen ):# app goes to background
2225self ._has_foreground = False
26+
2327def onStop (self ,screen ):
2428pass
2529def onDestroy (self ,screen ):
@@ -65,9 +69,8 @@ def finish(self):
6569def has_foreground (self ):
6670return self ._has_foreground
6771
68- def thc (self ,a ,b ):
69- #print("thc called")
70- self .counter = 0
72+ def task_handler_callback (self ,a ,b ):
73+ self .throttle_async_call_counter = 0
7174
7275# Execute a function if the Activity is in the foreground
7376def if_foreground (self ,func ,* args ,** kwargs ):
@@ -79,13 +82,13 @@ def if_foreground(self, func, *args, **kwargs):
7982#print(f"[if_foreground] Skipped {func} because _has_foreground=False")
8083return None
8184
82- counter = 0
8385# Update the UI in a threadsafe way if the Activity is in the foreground
86+ # The call may get throttled, unless important=True is added to it.
8487def update_ui_threadsafe_if_foreground (self ,func ,* args ,important = False ,** kwargs ):
85- self .counter += 1
86- if not important and self .counter > 250 :
87- # print(f"skipping because self.counter is {self.counter} and not important ")
88- return
88+ self .throttle_async_call_counter += 1
89+ if not important and self .throttle_async_call_counter > 100 : # 250 seems to be okay, so 100 is on the safe side
90+ print (f"update_ui_threadsafe_if_foreground called more than 100 times for one UI frame, which can overflow - throttling! " )
91+ return None
8992# lv.async_call() is needed to update the UI from another thread than the main one (as LVGL is not thread safe)
9093result = lv .async_call (lambda _ :self .if_foreground (func ,* args ,** kwargs ),None )
9194return result