Load screens from variable containing screen name

Description

How to load screens dynamically when the screen name is stored in a variable

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

ESP32-S3

What LVGL version are you using?

8

We know we can load screens like this with a fixed screen name (“screen1” or “clock” etc…)

lv_scr_load(screen1);

But can we replace the name of the screen, “screen1” with a variable that contains “screen1” or “screen2” or “lastScreen” that all match screens we’ve created?

Something like this:

screen0 = lv_obj_create(NULL);  // Create a blank 
screen1 = lv_obj_create(NULL);  // Create a blank 
screen2 = lv_obj_create(NULL);  // Create a blank 
//etc...

int currentScreenNumber = 0;
int nextScreenNumber = currentScreenNumber++;
char[] = "screen" + nextScreenNumber;
lv_scr_load( screen );

as you can see
lv_scr_load() has one arg of lv_obj_t * type - so this is not screen name, but screen object identifier. So you can just create array
lv_obj_t * screens[10];
fill it

screen[0] = lv_obj_create(NULL);
screen[1] = lv_obj_create(NULL); 
screen[2] = lv_obj_create(NULL);
...

then load screens
lv_scr_load(screens[currentScreenNumber++]);

Very cool, thank you! LVGL continues to impress me!