Beginners questions about activity life cycle, process_globals and globals.

Eduard

Active Member
Licensed User
Longtime User
An activity can be killed by the OS to free memory. This is new to me, and I have some errors in my program because of this feature.

Questions that I have:
  1. If an activity is killed, it's Globals can/will off course be removed from memory. How about public variables in Process_Globals? Will they remain declared? Or will they just lose their value? Or will they get their initial value back if declared like: public x=42 as int.
  2. If a variable is declared public in module X, initialized in module Y and assigned a value in module Z. To which module does it belong?
  3. Since a code module isn't an object but just code: to which module do variables belong that are declared in code modules?
  4. I read that a service module will be killed only after all other activities are killed. Does this include the current active activity? Is a service module therefore the best place to declare application wide globals? What if if I declare a public variable inside process_global inside the service and initialize it within activity Main. Does this variable than holds it value if Main is killed?
  5. Is activity 'Main' just an activity like all others regarding activity life cycle? Or will it be killed later than other activities to free memory? It is special in any way but it's name?
 

Eduard

Active Member
Licensed User
Longtime User
Thanks!

You're very helpful

If the user goes back to the app shortly after the process is killed to free up memory. Will the app always be started up from activity Main or is it possible that the last activity that the user was using is recreated and resumed even though process globals lost their value?

In other words: can I declare for example a large instance of a class as public process global in any module and give it some values in activity Main (because this is started first) and than use this class instance in any other activity without worrying about the OS cleaning it up?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If the process is killed then the main activity will start when the user presses on the application icon.

Note that an application can also be restarted from a service. The best solution is to have a code module that is responsible for the initialization. Something like:
B4X:
Sub Process_Globals
 ...
 Private initialized As Boolean
End Sub

Public Sub Init
 If initialized = True Then return
 initialized = True
 'Initialize the objects
End Sub

This allows you to call Init from each module that can start the application.
 
Upvote 0
Top