Initializing class in Process_Global error

Penko

Active Member
Licensed User
Hello!

This might sound stupid but let me explain and try to prevent the negativism.

I have a Code Module "AppSettingsStyles"

In process globals, I define multiple settings values(something I've inherited while learning from Klaus).

Let's see what I've got:

B4X:
'AppSettings code module
Sub Process Globals
Dim aCompaniesAddEditSettings As tActivityProperties
' aCompaniesAddEditSettings.Initialize(Colors.Yellow, "", 20, Colors.Red) ' this doesn't work because it produces a compile error(read below).

End Sub
However, if I do something like this:

B4X:
Sub setInit()
   
   aCompaniesAddEditSettings.Initialize(Colors.Yellow, "", 20, Colors.Red)

End Sub

It compiles just fine.

It's better for me to stick the values initialization to the declaration as it will be faster to change anything.

But is there a serious reason NOT to declare it there? I've done it because it's app settings and I just need to access them from every activity.

If, interested, here is what tActivityProperties is.

B4X:
' I don't see any ActivityContext here. I even tried this(Dim dummy as View) and the compile process is broken in a different way.

'Class module
Sub Class_Globals

'   Dim dummyView As View
   
   Dim P_BgColor As Int
   Dim P_BgImage As String
   Dim P_TextSize As Int
   Dim P_TextColor As String
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(BgColor As Int, BgImage As String, TextSize As Int, TextColor As String)

   P_BgColor = BgColor
   P_BgImage = BgImage
   P_TextSize = TextSize
   P_TextColor = TextColor

End Sub

Public Sub BgColorPro As Int
   Return P_BgColor
End Sub

Public Sub BgImagePro As String
   Return P_BgImage
End Sub

Public Sub TextSizePro As Int
   Return P_TextSize
End Sub

Public Sub TextColorPro As Int
   Return P_TextColor
End Sub

I read such errors by other people. Erel, you told them to verify they have no old versions of internal libraries. I can be my no means sure I don't have some but I made install in a different directory so that shouldn't be the case, at least according to my opinion.
 

Attachments

  • compile.jpg
    compile.jpg
    91.3 KB · Views: 162
Last edited:

Penko

Active Member
Licensed User
Hello Erel!

I created a simple project trying to reproduce the AHLocale issue I described in another thread but couldn't do it. However, I've added a process global varialbe in Activity "Second" which uses a Class Type.

It now even creates a "Force Close" exception which is not what happened before that.

I know you've said not to use any code apart from Dim statements in Process Globals but when I have a more complex data type, I can't do it another way :).

See the attached small project.
 

Attachments

  • AHLocaleExample.zip
    11.3 KB · Views: 96
Upvote 0

Penko

Active Member
Licensed User
Because I am not sure this value will be first needed. Maybe doing it in Main will do the job but what if the user closes the app from another activity and gets back after a long pause(isfirst is true)? Then I should get back to main to initialize again or call this initialization from every activity. It is still a problem for me to handle process globals safely. I suceed in doing SQL and Locale translation on demand(not when I start the app) but process globals are a problem because I may load different value during the program execution. So, do process globals loose their value after some point, say a big device sleep?

Sent from my Next7P12 using Tapatalk 2
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
Process globals lose their value in one case, the whole process is killed. In that case the next time that an activity is displayed FirstTime will be true.

A good way to handle such variables in an app made of multiple activities is to put them in a code module with an Init sub:
B4X:
Sub Process_Globals
  Public Some_App_Wide_Variable
  Private alreadyInitialized As Boolean
End Sub

Sub Init
 If alreadyInitialized Then Return
 alreadyInitialized = True
 ... 'Initialize variables
End Sub

Then you are safe to call this Init sub from all activities.
 
Upvote 0
Top