Android Question Preferred way to have global objects?

Troberg

Well-Known Member
Licensed User
Longtime User
I have an object which contains all the global stuff that needs to be available to the entire app. However, among that information is a list of objects that listens to messages, among them the Main activity, in which this global object is declared.

The compiler don't like this, as you can't have activity stuff in Process_Globals.

What is the preferred way of handling this scenario?

Also, what is the difference between Activity and Me (in an activity)?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Activities are destroyed and recreated during the life time of your app. You should treat them as a temporary objects.
Services, on the other hand can keep on running as long as the process is alive. You should move all the global stuff that isn't tied to the UI to a service (or a class in the service).

Also, what is the difference between Activity and Me (in an activity)?
Activity is a reference to the main UI element. Me is a reference to the code module (Main for example).
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
Activities are destroyed and recreated during the life time of your app. You should treat them as a temporary objects.
Services, on the other hand can keep on running as long as the process is alive. You should move all the global stuff that isn't tied to the UI to a service (or a class in the service).

I'll look at that. However, in my case, it's very much an always front, always on, single activity app, so it might not be needed.

Activity is a reference to the main UI element. Me is a reference to the code module (Main for example).

Ah, nice. That solves the immediate problem, as it's the code module I actually need a reference to. Everything that might manipulate the activity will go through public methods in the code module.
 
Upvote 0
Top