Android Question Classes, Code Modules Suggestions

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I am working on a little project and just wondering what is the best way to do this..

First of all I am going to have:
- approx. 5-8 activity's
- 1 TCP Socket connection (always connected only while the app is open)
- 1 Database (SQL Lite) to store connection settings etc.

Now here is what I was planning to do and want some suggestions if this is going to work or going to cause issues..

Activity's:
1. Each of the activity's needs to connect to the TCP socket to be able to send data to it.
2. Each of the activity's needs to be able to connect to the SQL Lite database.

What I am planning to do is..
The Class Module will connect to the SQL database and will also create the connection to the TCP device.
The Class module is going to store all incoming data from the TCP device and will then save some information into the SQL database as well.

I am planning to use the Class Module and a Code module as the hub of my app.
If I Dim my class module in the code module and then reference and call all things using the code module would that work or would I need to keep Initializing it all the time?

For Example:
My Class Module (MyClassHub):
B4X:
'Class module
Sub Class_Globals
	' Dim my socket connection here
	' Dim SQL Lite database here
	Dim Names(100) As String
	Dim Devices(250) As String
End Sub

'Initializes the object. 
Public Sub Initialize()
	' Initialize the SQL database as soon as the class is created
End Sub

Sub Connect
	' Sub that will connect to my TCP Socket
End Sub

Sub Disconnect
	' Sub that will disconnect my TCP Socket
End Sub

Private Sub AStreams_NewData (Buffer() As Byte)
	' all new data comes here
End Sub

Sub Save_to_database(Query as string)
	' save to the SQL Lite database
End Sub

Code Module (Demo):
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	Dim test As MyClassHub
End Sub

Sub Connect_Socket
	test.Connect
End Sub

Sub Disconnect_Socket
	test.disconnect
End Sub

' call other functions from the class (test)

Then from each of the Activity's I would call functions from the code module.
If I Initialized the class on each of the Activity's it would need to connect to the TCP device again where if I Initialize it from the code module I won't have to keep Initializing it as it will be created already and will stay open.

Would the above work by Initializing the class in a Code Module and calling the functions in the Code module from my Activity's?
Or does anyone have any other suggestions on how I can do this, as I want to do this from a central location if possible so all my code is in one module.

The only other way I was going to do this was rather then use a Code module, use a Service module instead and call my class from the service, and rather then creating the connection in the class, create the connection in the service module.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use a Service for the TCP connection. Classes share the context of their parent (activity or service). This means that if you initialize the class from an activity, the class will only receive events while the activity is visible. It will be much simpler to use a Service which will just run all the time.
 
Upvote 0
Top