Update a shared variable within another screen

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

Raspberry PI4, GCC 8.3.0

What LVGL version are you using?

7.9

What do you want to achieve?

  1. From screen A, create a new screen B
  2. Do something within screen B and update a global shared variable
  3. Re-load parent screen A
  4. Check (new) value of shared variable

What have you tried so far?

Declared top-level variable V in screen A source module
Draw screen B (calling an exported function of B source module)
B source module has “extern V”
Update V within B
Re-load parent screen A with “lv_scr_load(parentScreen)” (parentScreen is passed to B draw method)

Code to reproduce

I’m unable to submit a code that compiles.

Summary:

ScreenA.c

[top-level] int g_presetNameEntered = 0;
[...]
drawSaveNewPresetDialog(lv_scr_act());
if (g_presetNameEntered) {...Never Enters Here... }

ScreenB.c

[top-level] extern int g_presetNameEntered;
[...]
void drawSaveNewPresetDialog(lv_obj_t* pParentScreen)
{
  g_presetNameEntered = 1;
  lv_scr_load(g_parentScreen);
}

I’m accustomed to something like “onLoad()” which would be called as first method when a page is loaded (I would check “g_presetNameEntered” there), but I reckon there’s nothing like this in LVGL.

So which direction should I take?

Thanks!

No suggestions?
At the moment, I am resorting to modal message boxes (so I do not jump between different screens), but it is a bit limiting.

Thanks

In this case the best thing to do would be to have an updateScreenA() function which you call whenever screen A is (re)loaded. That function should check the global variable and update any UI elements accordingly.

Yes, but how would i “trigger” this function on screen load? I mean there’s no “onscreenload” event as far as I know, right?

Correct. You would have to call it manually whenever you load the screen; there is no event for finding out when a screen gets loaded.

I found that having a central function to swap screens is simple and intuitive. Simplify define N screens and call a function to switch to the next one. That function can also call an ‘on_unload()’ function of the old screen and an ‘on_onload’ of the new screen. It’s easier with C++ but can also done with C structs and function pointers.

In my case, I can create all the screen when the program starts and then switch them as needed.