Skip to content

Commit 286bd76

Browse files
Improve app lifecycle documentation
1 parent 65c6924 commit 286bd76

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

docs/apps/app-lifecycle.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,17 @@ def on_download_complete(self, data):
287287

288288
### Thread-Safe UI Updates
289289

290-
When updating UI from a background thread, use `update_ui_threadsafe_if_foreground()`:
290+
When updating the UI from an asyncio task/coro, there's no need for special handling, as asyncio runs in the same thread as LVGL.
291+
So that's the easiest way to do multitasking.
292+
293+
But sometimes, you want to do an action in a completely new thread, created with, for example:
294+
295+
```
296+
_thread.stack_size(TaskManager.good_stack_size())
297+
_thread.start_new_thread(self.background_task, (an_argument, another_one))
298+
```
299+
300+
Then, to update the LVGL UI from the other thread, use `update_ui_threadsafe_if_foreground()`:
291301

292302
```python
293303
def background_task(self):
@@ -301,6 +311,11 @@ def background_task(self):
301311
)
302312
```
303313

314+
This will schedule the function `self.display_result(result)` to be executed from the LVGL thread, using `lv.async_call()`.
315+
It will also check that the Activity is still running in the foreground, using `has_foreground()`.
316+
317+
For this to work, the `onResume()` and `onPause()` functions need to run their super(), either implicitly (if you don't override them) or explicitly (if you do), as explained before.
318+
304319
## Complete Example
305320

306321
Here's a complete example showing proper lifecycle management:

0 commit comments

Comments
 (0)