Advice on integration of simple GPS

SteveBee

Member
Licensed User
Longtime User
I am developing a data update form, to be used 'in the field'. It occurred to me to add the Lat/Long co-ordinates when the 'save' button is pressed.

  • Having added the Lib, I added in GPS as a ProcessGlobal, and Initialise it in the Activity_Create/FirstTime of the main (first) screen.
  • I have a test for GPS-enabled in the Activity_Resume of this first screen (also works as a re-test, every time this home screen is returned to).
  • The update itself is in a second activity; on Activity_Resume of this second Activity, I issue a Start(0,0), with LocationChange writing to more ProcessGlobal variables
  • When the 'save' button is pressed in this second/update Activity, I include the current values of Lat/Long (last updated by LocationChange) in the update list
  • On Activity_Pause in the second/Update form I issue a GPS Stop

Given that most time will *not* be spent in the update form, battery use should not be OTP.
Does this approach seem OK?

Steve
 

SteveBee

Member
Licensed User
Longtime User
You should add the GPS and initialize it in the update form activity. The GPS events will not fire while their Activity is paused.

But if it's declared in Process_Globals (as I have) then surely it's *always* available, no matter what module it was declared in?

Similarly, once it's initialized, then it's good to go (being a Process_Global) - no matter where initialized, yes?

In my description the second Activity *is* the update form - and thus is where the location_Changed handler is situated.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Similarly, once it's initialized, then it's good to go (being a Process_Global) - no matter where initialized, yes?
No. While you can access its properties and methods from all modules, it will not raise any event when its activity is paused. The Sub that handles the event must also be located in the activity that contains the object.
Activities are independent components which have their own life cycle.
You can use a Service to host the GPS object and handle the event (passing the coordinates to the required Activity using CallSub). If I understand your needs correctly, it will be better to host the GPS object in the second Activity.
 
Upvote 0

SteveBee

Member
Licensed User
Longtime User
Ok, Erel, I see... trickier than it seems.

I've read and re-read your piece on the 'life-cycle', but I would never have come to that conclusion re. the GPS object. It would probably be good to add this example to that help-document.

Yes, you're quite right, the second module is the place to put the GPS.
And, I think not locate it in Process_Global - since it's use will now be entirely 'local'? But I would use Activity_Create / FirstTime = True to initialise it?

Can you provide more information on the background here? As I say I read the life-cycle piece, thought I understood it, but... you write:

"The FirstTime parameter tells us if this is the first time that this activity is created. First time relates to the current process."

But if a 'Paused' activity can be destroyed by the o/s, why shouldn't 'first time' initialisations be required then too? Because what part of the object can remain 'initialised' once the o/s destroys it?

Thanks for the insight.
Steve
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
"First time" relates to the first time the individual activity is instantiated in the application process. Typically you will initialise Process_Globals here and thereafter they will remain instantiated/initialised until the process is destroyed. Process_Globals are static and will survive until the application process is destroyed. As they exist outside of the context of an individual activity, which is the only type of module that can support UI elements, process globals cannot be UI elements.

Globals are not static and exist only as long as the activity to which they belong exists hence they may be UI elements. Typically you instantiate/intialise globals in Activity_Create or instantite/restore saved UI state if not the first time and save UI state in Activity_Pause in case the activity is destroyed.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
And, I think not locate it in Process_Global - since it's use will now be entirely 'local'? But I would use Activity_Create / FirstTime = True to initialise it?
I recommend you to declare it in Process_Global and initialize it if FirstTime is True.
You can call Gps.Start / Gps.Stop in Activity_Resume / Activity_Pause.
 
Upvote 0
Top