Basic4android v1.2 includes a new type of module which is the static code module.
Adding a static code module is done by choosing Project - Add New Module - Code Module.
Unlike Activities and Services, code modules are not related in any way with Android process life cycle. These are just containers for code.
All of the subs in these modules are public and can be accessed from other modules.
Code modules use cases include:
- Avoiding duplicate code in multiple modules.
- Sharing code between projects. For example you can create a code module that parses some file. Later you can easily reuse this module in other applications.
- Separating your application logic. Each code module can be used for a specific task. This will make your program clearer and easier to maintain.
As a code module is not tied to an activity or service it uses the calling component context when required.
For example, calling a code module sub that shows a Msgbox from an activity will work. However if you call it from a service it will fail as services are not allowed to show dialogs.
Code modules cannot catch events.
While you can use a code module to initialize a button for example:
From the activity module you can call:
Now in order to catch the click event you should create a sub named Button_Click.
This sub should be located in the Activity module, as Code modules cannot catch events.
CallSub which internally uses the events mechanism cannot be used to call code module subs (which can be called directly instead).
Adding a static code module is done by choosing Project - Add New Module - Code Module.
Unlike Activities and Services, code modules are not related in any way with Android process life cycle. These are just containers for code.
All of the subs in these modules are public and can be accessed from other modules.
Code modules use cases include:
- Avoiding duplicate code in multiple modules.
- Sharing code between projects. For example you can create a code module that parses some file. Later you can easily reuse this module in other applications.
- Separating your application logic. Each code module can be used for a specific task. This will make your program clearer and easier to maintain.
As a code module is not tied to an activity or service it uses the calling component context when required.
For example, calling a code module sub that shows a Msgbox from an activity will work. However if you call it from a service it will fail as services are not allowed to show dialogs.
Code modules cannot catch events.
While you can use a code module to initialize a button for example:
B4X:
Sub ButtonsCreator(Text As String) As Button
Dim b As Button
b.Initialize("Button")
b.Text = Text
Return b
End Sub
B4X:
Activity.AddView(CodeModule.ButtonsCreator("press here"), 10dip, 10dip, 200dip, 200dip)
This sub should be located in the Activity module, as Code modules cannot catch events.
CallSub which internally uses the events mechanism cannot be used to call code module subs (which can be called directly instead).