Skip to first unread message
Assigned to ewpa...@gmail.com by steve....@gmail.com

charles_...@hotmail.com

unread,
Mar 21, 2017, 10:09:09 PM3/21/17
to MIT App Inventor Forum

The documentation states that Global Variables are available anywhere in an app, by using Get. However, if an app has multiple screens, Get only shows the variables from the current screen and ignores the variables from another screen. How are truly global variables declared that are available from on screen?

Abraham Getzler

unread,
Mar 21, 2017, 10:27:32 PM3/21/17
to MIT App Inventor Forum
That's what TinyDB is for.

Instead of SET GLOBAL VariableName use 
Save in TinyDB tag(variablename in text) value ()

Instead of Get Global VariableName use
Get From TinyDB tag(variablename in text) default(value you would use for initialization)

You will have access in all screens, plus the extra benefit of storage between runs of your app.

ABG

SteveJG

unread,
Mar 22, 2017, 8:38:59 AM3/22/17
to MIT App Inventor Forum
Global Variables are not truly global to the entire  App in AI2.  They are only available on the screen they are initialized.  Global is somewhat of a misnomer.  The values of this type of variable can be passed to another screen using a TinyDB but then the 'variable' must be re-declared (initialized) on that screen to be able to use the value on that screen.

Each Screen in App Inventor works like an independent app.  Each screen uses memory allotted to it until released (closed). It has its own set of 'global' variables and can also use 'local' variables.

Unless needed for a special purpose, the 'experts' on this forum generally recommend not using multiple screens because if you pass variables apparently simple coding becomes complex (because of the need for a TinyDB, creation of 'parallel' variables on the multiple screens etc.   It is for this reason we recommend using multiple vertical layouts (or horizontal) that are hidden or shown using their Visible property (true or false) to simulate multiple screens.  If you simulate the screens, you do not need a TinyDB and in many ways your coding usually is simpler.

The thing to remember is global variables are ONLY global between Procedures on the screen they are created.

This is what the MIT App Inventor documentation says about global variables:

initialize global name to

This block is used to create global variables. It takes in any type of value as an argument. Clicking on name will change the name of this global variable. Global variables are used in all procedures or events so this block will stand alone.

Global variables can be changed while an app is running and can be referred to and changed from any part of the app even within procedures and event handlers. You can rename this block at any time and any associated blocks referring to the old name will be updated automatically.

get

This block provides a way to get any variables you may have created.



The documentation does not state "that Global Variables are available anywhere in an app, by using Get."      Unfortunately the documentation does not indicate that a global variable cannot be used between screens without 'passing' the value to a similar variable on a subsequent screen. 



Regards,
Steve

charles_...@hotmail.com

unread,
Mar 22, 2017, 9:34:58 AM3/22/17
to MIT App Inventor Forum
Thanks for that Steve.

Now that I know I have to use TinyDB things are straightforward from here.

I don't want to be picky - there is nearly always a but after that statement - but the documentation does say that global variables: "...can be referred to and changed from any part of the app..." and Get also states that it: "...provides a way to get any variables you may have created." so an edit may be appropriate here.

Thanks again

Charlie

Taifun

unread,
Mar 22, 2017, 10:20:22 AM3/22/17
to MIT App Inventor Forum
see the following general tips and in your case especially #1

  1. Use different screens wisely
    Before starting to create another screen, first you should think about is it really necessary? See also Building apps with many screens and SteveJG's post about advantages/disadvantagesbecause in only one screen you also can use vertical arrangements to simulate different screens, just set the arrangements to visible = true/false as needed... See also Martyn_HK's example about how to use Tabs in App Inventor and another example from Cyd
          
    The recommended method of switching screens in App Inventor

  2. App Inventor works best if you use images whose size matches the size you want them to appear on your screen. If you import larger images into your app, your app may run out of system memory. Using Images with App Inventor

  3. Avoid redundancy
    Probably it helps to read chapter 19 - 21 in Dave's book http://www.appinventor.org/book2 to get an idea how to do DRY programming with App Inventor - Don't repeat yourself 

  4. See SteveJG's monster list of tips and tricks

  5. How to overcome the App Inventor project limit of 10 MB

  6. Backup your project frequently
Taifun

Trying to push the limits of App Inventor! SnippetsTutorials and Extensions from Pura Vida Apps by  Taifun. 

charles_...@hotmail.com

unread,
Mar 23, 2017, 10:58:40 AM3/23/17
to MIT App Inventor Forum

Thank you Taifun.


All very helpful advice. I have changed to 3 vertical  arrangements.


Now I have to work out how to design an order form that will allow the user to select items and quantities, with prices  and totals.


I can do that in MS Access, now I need to do it within an app, without reference to the cloud, then send the result with JSON.


I will be experimenting with List Pickers, Spinners and Text Boxes.


Many thanks


Charlie

Abraham Getzler

unread,
Mar 23, 2017, 12:53:13 PM3/23/17
to MIT App Inventor Forum
 then send the result with JSON

There's a Designer flag in Screen1 that will automatically convert JSON objects and arrays
into JSON format text on assignment into text format, for example label .text or the
text JOIN block.

I've attached a sample app that can emit JSON text with depth limitting and pretty indentation
all in blocks.  It depends on recursion, though, which might fail depending on the shape
of your data.

ABG

JSON_encoder.aia
blocks.png

charles_...@hotmail.com

unread,
Mar 23, 2017, 10:28:50 PM3/23/17
to MIT App Inventor Forum
Thanks for that Abraham

Its a great help.I am in the happy situation of a bit of information overload, so it looks like a busy weekend ahead, designing my input form and creating the JSON output..

The JSON data will be pretty simple, with just: Order type, Customer ID, Date/Time stamp plus the order lines with Product ID and Quantity.

Many thanks for your generosity.

Best regards

Charlie
Reply all
Reply to author
Forward
0 new messages