Android Question "Scope"

LucaMs

Expert
Licensed User
Longtime User
[I could try it by myself, but since at this moment there is a very expert person online and I'm too lazy... :D]

I have (maybe) a global instance of a my class in the Starter service.
I need to use it in more than one other "modules", an Activity, for example.

What if I declare a Private variable in the Process_Globals of the Activity and I set it with a reference to the Starter object?


[I hope it is clear :(]


B4X:
' Starter
Public GlobalObj As MyClass

Sub Service_Create
    GlobalObj.Initialize

B4X:
' Activity
Sub Process_Globals
    Private mObj As MyClass

Sub Activity_Create
    mObj = Starter.GlobalObj

I think this should work. (to avoid to write Starter.GlobalObj.SomeMethod each time :))
 

LucaMs

Expert
Licensed User
Longtime User
Despite my laziness, I have tried and it works, as expected.

Useless question (but maybe useful topic)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I prefer to use Globals only to declare views, but you're right, Klaus, I could declare that object in Globals.
 
Upvote 0
Top