More often than not, I prefer to create my own libraries using the BA IDE and the Compile To Library option in the Project sub-menu, to using Eclipse and Java.
One of the disadvantages of developing classes in B4(A/J) instead of Java, is the restriction to only one Initialize(r), as per the documentation and confirmed by Erel's statement. Sure the initializer may accept arguments as well, but at the end, there can be only one initializer per class. Fortunately, there are various ways of lifting that restriction. One is demonstrated in the code below:
To allow this to happen in a pure BA class you must ensure that your extra initializers call B4A's private method innerInitialize.This is taken care of by the sub InternalInitialize that makes a reflection call to do just that.
Therefore by calling InternalInitialize at the beginning of your extra initializers you get the benefit of multiple initializers without having to create your libraries in Java. (It's possible to call your own class Initializer as well and therefore benefit from multiple initializers. I'll describe this method later on.)
Edit: 4/12/2015
Following some of the comments this post received, I realize that I should have made it clear what the class initializer does:
Initialize does two main things:
Second Edit:
I have updated the code in all my posts to remove the hard coded value of ba (i.e. context).
In the original post, it was set to 'main', which is not necessarily the correct context as you may Dim and Initialize the classes in other activities or services.
One of the disadvantages of developing classes in B4(A/J) instead of Java, is the restriction to only one Initialize(r), as per the documentation and confirmed by Erel's statement. Sure the initializer may accept arguments as well, but at the end, there can be only one initializer per class. Fortunately, there are various ways of lifting that restriction. One is demonstrated in the code below:
To allow this to happen in a pure BA class you must ensure that your extra initializers call B4A's private method innerInitialize.This is taken care of by the sub InternalInitialize that makes a reflection call to do just that.
B4X:
Private Sub InternalInitialize(ba As Object)
Dim r As Reflector
r.Target = Me
r.Runmethod4("innerInitialize", Array As Object(ba), Array As String("anywheresoftware.b4a.BA"))
End Sub
B4X:
Sub Class_Globals
Private Language As String
End Sub
Public Sub Initialize
Language = "English"
End Sub
Public Sub Initialize2(ba As Object, language_translation As String, some_extra_parameter As Double)
InternalInitialize(ba)
'
Language = language_translation
'.....
End Sub
Private Sub InternalInitialize(ba As Object)
Dim r As Reflector
r.Target = Me
r.Runmethod4("innerInitialize", Array As Object(ba), Array As String("anywheresoftware.b4a.BA"))
End Sub
Following some of the comments this post received, I realize that I should have made it clear what the class initializer does:
Initialize does two main things:
- It sets the object's context (the parent activity or service, ie ba object).
- It calls the Sub Class_Globals so that the variables are available to the object
Second Edit:
I have updated the code in all my posts to remove the hard coded value of ba (i.e. context).
In the original post, it was set to 'main', which is not necessarily the correct context as you may Dim and Initialize the classes in other activities or services.
Last edited: