Global variable scope

Hi all!

I have a couple of questions about how global variables work.

  • Are global variables declared on one screen only known to that screen, or can they be accessed from another screen's blocks?
  • Are global variables declared on one screen re-initialized each time the screen displayed?

Here are my blocks:

What I am trying to do is, each time the app is started, try to connect to one specific BlueTooth device - 'ESP32test'. I am using a flag, 'Global BT_attempt' to test if the app has tried to connect to the device.

I have a second screen (Screen2) for app settings, and when I go to that screen and return to the main screen (Screen1), the app again attempts to connect to the device. So it appears, 'Global BT_attempt' is initialized each time the screen is displayed.

Is there anyway to have the 'Global BT_attempt' retain it's value when switching screens?

In c programming, I'd declare this to be a 'static boolean' variable. Does ai2 have anything similar to this?

Thanks for any help!

You will need to store the value in TinyDB transport it/access it on another screen


HOWTO: Transfer data between screens using TinyDB Demo

Thanks for the help and suggestions, but it's not quite what I am looking for.

I am looking for a way to have something happen (try connecting to bluetooth device) once when the app is first started. I want this to happen only the first time screen1 is initialized, and not every time screen1 is initialized.

does that make sense?


Use a tinydb tag with a boolean value. Initially the tag is set to false, but after first run of app is set to true.
You test this tag each time the app starts. If the tag is false then it can run a procedure, if the tag is true then it will do nothing.

1 Like

It can't be that way. Changing the screen from Screen1 to Screen2 will always disconnect BT. You can make it connect automatically on subsequent screens without the intervention of the application user. This is proof that using multiple screens is a bad idea. I use arrangements instead of screens.

@TIMAI2 I'm not sure I follow you.

Something like this:

That doesn't work because the tinyDB value would need removed when the app closes, and I'm not seeing a way of doing that.

Please explain, my app will loose it's BT connection when I change screens?

If so, I will have to look into arrangements.

Thanks for the help,

Like so


When you go from Screen1 to Screen2, and then you come back to Screen1, you will discover that Screen1 remembers the values of its own variables (those defined in Screen1). But the variables on the other screens are reset every time you change screens.

If what you want, is to have access from Screen2 to the Screen1 values, it doesn't work to define in Screen2 variables with the same name (even if they have the same name, they are different variables across screens).
To accomplish this, before switching screens, you have to save those values in the mobile local storage (under labels using the TinyDB component), and then, when you are on the new screen, you have to recall the content of those labels, by reading the local storage again with TinyDB.

Other possibility is sending a single value between screens:
For sending a value from Screen1 to Screen2, you can use in Screen 1 the Open another screen with start value block, and then you can fetch the value in Screen2 with get start value

When you want to send a value from Screen2 back to Screen1, then you first use in Screen2 the close screen with value block . And then, when you are back in Screen1, you fetch the value with the result parameter of the block when Screen1 .Other screen closed.

As you can see, it's not trivial.
And it gets more and more complicated with more than 2 screens.
That's the reason why it is normally preferable (instead of swaping screens) to use a single screen, but simulate that your app has different screens by showing or hiding different Vertical Arrangements in the same app inventor screen.

I hope this explanation helps someone else, Because English is not my first language, and it took me a lot of reading documentation and searching in the community for expert's help, plus some experimenting, to understand this swapping screen mechanisms and those blocks behaviour..

Your post was just a good summary of what has been discussed in the topic :grin:

You could have written in your native language,
there is no only English rule here.

Plus we have an inbuilt Yandex translator in the community!

To circumnavigate the BT disconnecting when changing screens, I used layout boxes as my screens and made them visible/invisible when a button is pressed, you have to be clever about it tho