How to call a function after screen is displayed. BUG on LV_EVENT_SCREEN_LOADED?


Hello everyone, and thank you in advance for any responses! :blush:

I’m encountering an issue where I want to call a function immediately after a screen is displayed. I’ve discovered the LV_EVENT_SCREEN_LOADED event, which seems to fit the bill.

Here’s what I’ve done so far: I’ve added this event to the screen I want to call the function after it’s loaded (ui_Screen2). Then, in the function (ui_event_Screen2), I’ve placed the code for the function I want to execute.

However, the problem I’m facing is that the function gets called not only when the screen is initially loaded (which is correct) but also when I navigate away from this screen to a new one.

Can anyone advise on how I can achieve the desired behavior? What might I be doing wrong, or what aspect of this mechanism have I misunderstood?

Thank you once again for your assistance!

What MCU/Processor/Board and compiler are you using?


What LVGL version are you using?


What do you want to achieve?

Call the function ui_event_Screen2 only on screen load.

What have you tried so far?

I have tryied to disable the second execution with a boolean variable but it let me crash the system.
If I comment the lv_scr_load_anim the function is correctly called once.

Code to reproduce

Not provided

Screenshot and/or video

Images to explain better.

The Screen1 rapresent in the first part the function called on setup whic append the function ui_event_Screen2 to the LV_EVENT_SCREEN_LOADED event. In the second part there’s the function that is called twice.



The Screen2 rapresent the SerialMonitor used for debugging.
I have added a random number generate from the function to help identify the order of what is called and i have placed a Serial.println to replace my function call.



Sorry, but call delay 1000 or any delay inside code is nonsense.
And good practice is place if check inside cb. Somethiink as

    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    if(event_code == LV_EVENT_SCREEN_LOADED) {

PS Seems your code is maybe from Squareline, then yuo all this can generate inside events designer.

Hi Marian,

The delay(1000) call was only for debugging purposes to illustrate what was happening within the code over time. Anyway thanks.

Regarding the event_code, it consistently remains LV_EVENT_SCREEN_LOADED because I’ve bound the invocation of ui_event_Screen2 only to that event, as indicated in the code snippet provided earlier.

Even with the inclusion of the if condition, the behavior remains unchanged. I’ve given it a try, but unfortunately, it doesn’t alter the outcome.

Then check if you dont have this twice in code. SQS place same func inside ui_init… I preffer search all files for lv_obj_add_event_cb

Already done.
I have placed for debugging purpose a Serial.print before the lv_obj_add_event_cb and it is called only once.
Already search for lv_obj_add_event_cb but there’s only one time with the binding of this function.
It seems that (as log show) the event LV_EVENT_SCREEN_LOADED be called twice, first when the screen is loaded (correctly) and the second when the animation to exit the screen is started (not expected).

If i comment the line of lv_obj_add_event_cb the function is never called, so it is binded only once and in that point.

Then maybe anim load screen invoke second loaded, im not sure i use 8.3.7 and SQS invoke method where is

void ui_event_Screen0(lv_event_t * e)
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    if(event_code == LV_EVENT_SCREEN_LOADED) {
        _ui_screen_change(&ui_Screen1, LV_SCR_LOAD_ANIM_FADE_ON, 1200, 0, &ui_Screen1_screen_init);

I’ve attempted to switch to version 8.3.7, and it appears that the issue of the function being called twice has been resolved. It seems there might be a bug specific in version 8.3.11.

Marian, could you please try temporarily switching to version 8.3.11 to confirm whether you encounter the same issue? You can do this by placing a Serial.print statement inside the ui_event_Screen0 function and checking if it appears twice.

If we can both confirm the double calling behavior in version 8.3.11, it would be great to open a bug report to address this issue.

Thank you for your cooperation.