Problem with TinyDB and multiple screen

On scren1 it call a TinyDB variable with a default (if it does not exist of 1), this is done on the screen1.init event.
Then I call for screen 2 where it set this variable to 2 (I verified that it has the right value).
When I exist screen 2 (and go back to screen 1 hopefuly on screen1.init event too) the variable is still 1.
But when I exist the application and reenter it, it get the variable (on screen1) at 2.

Any idea ?

I already checked that between the two screens the:

  • the names of the database are the same.
  • the namespaces of the databases are the same.
  • the names of the variables are the same.

It seems that I dont have the same problem while running under the AI2 application than when creating an APK (where the problem stands)


One of the annoyances of working with multiple screens is having to worry about the screen.Initialize event and the Other Screen Closed events.

Which event will need the responsibility to reload the global variable from TinyDB? Maybe both?

I avoid this by not bothering to use a global variable for a TinyDB value, since it has the direct read and write blocks. I just use TinyDB directly, and maybe a local variable if need for an update in place.

Here bellow is a basic example:

Screen 1:

Screen 2:

It works fine on AI2 but not when APK.

You can pass values directly:
BetweenScreens_WithVars.aia (4.3 KB)



First of all, if you show a picture, make it by right clicking on the block and choose download block as png, or if you want to show all blocks, right click in an empty space and choose download blocks. Upload these!
Second, what exactly does not work, when you build an .apk?

I find your blocks really strange. In Screen1.Initialize you are going to store the value that you read??
Sounds like a circular movement.
Also, remember that when you close Screen2 like you do, Screen1.Initialize will not be run.
I guess what you really want in screen1.initialize is this:
blocks (42)

The block you could use to find out what happend in Screen2 is this:
component_event (2)

Finally, I happen to know French, but maybe more people would respond if you (temporarily) swich your blocks to English, see the dropdown at the top right corner.

Using TinyDb:
BetweenScreensTinyDB.aia (4.7 KB)



Using TinyDb is the best choice if several values need to be shared at the same time. If only one value, it's overkill.

Hi, thanks a lot to all of you for you pertinent and quick answers.

I was not aware of the OtherScreenClosed event, I will use it now!

In my application (Ghica) I have a lot of context to transfer from screen 2 to screen 1 and I want it to remain (from one call to the application to oneother, it is for example: mail adddress, phone number..) I dont want that the user re-enter it each time he call the application, this is why I need tinyDB.

It remains the point that the behavioral is different when using AI2 emulator to when it is APKised.

If I understand properly it means that when getting back from a child screen to its parent, in AI2 emulation we call Screen.Initialize event and not when APKised.
It is probabely (I havent check) the same for the Global variables I will have set in Screen1 before calling Screen2, they will be re-initialized in AI2 emulator BUT NOT if APKised. Could someone confirm/infirm ?


this is correct, see chapter "Some Notes" here The recommended method of switching screens in App Inventor

probably virtual screens are the better choice for you= see tip 1