Android Question Calling a Sub in Activity from Code Module

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I have read Erel's tutorial on CallSubDelayed from here: http://www.b4x.com/android/forum/th...teract-between-activities-and-services.18691/

What I am doing is this:

On a Activity (Main) I have a label (label1) and as soon as the Activity runs I am creating a SQL Lite database and loading a whole heap of tables and loading some default data into that table. (approx. 10 tables and 1 row in each table)

I am creating the database using the following code:

'Main' Activity:
B4X:
Sub Process_Globals
	Dim SQL1 As SQL 
End Sub
Sub Activity_Create(FirstTime As Boolean)
	If File.Exists(File.DirDefaultExternal,"my_database.db") = False Then 
		'database has not yet been created
		SQL1.Initialize(File.DirDefaultExternal, "my_database.db", True)
		mycodemodule.CreateDatabase
	End If
End sub
Sub DatabaseCreated
	Log("Data base is now created")
End Sub


'mycodemodule' Code Module:
B4X:
Public Sub CreateDatabase
	' Code here that creates the tables etc..
	' ..Create Table..
	' ..Load data into table..
	'Now once the tables are created I need to fire the DatabaseCreated sub from the Main Activity
End Sub

As you can see I need a way after it has created the tables etc in the CreateDatabase sub to trigger the DatabaseCreated Sub in the main Activity.

I have used CallSub(Main, DatabaseCreated) and it didn't work.
I then searched the forum and found CallSubDelayed(Main,"DatabaseCreated") and that worked.

However, after reading Erel's Tutorial, it says:
Note that you cannot use CallSubDelayed (or CallSub) with code modules.

Would you say since I have it working with CallSubDelayed that it will work on other devices? or is there another way I should be doing this?

The reason I am doing this in a Code module is because I maybe using this code again later in the app and don't want to have the same code multiple times in the app otherwise if I make a change I need to keep going back to multiple places to change the code.

The only other way I thought of doing this was to create a library and call that library and trigger the code from it and just make the change in the library and I only need to change it from there.
 

aaronk

Well-Known Member
Licensed User
Longtime User
I was running this in Debug(Legacy).

I seem to only get it working using CallSubDelayed(Main,"DatabaseCreated")

But after reading the tutorial Erel did I was wondering if I should be using CallSubDelayed or if it's going to cause errors later with other devices people have since he said don't use CallSubDelayed in code modules.

When I use CallSub(Main, DatabaseCreated) nothing happens. No errors, nothing.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
It Logs:

** Activity (main) Create, isFirst = true **
Activity is paused. CallSub will fail silently
** Activity (main) Resume **

Then when I end the app it says:
** Activity (main) Pause, UserClosed = true **

However the Main Activity is showing on the screen.

I think since I am calling the Code module from the Activity_Create sub its not fully loaded the app and it says its paused.
I then called the code from the code module from a button, so I can let the app load first before running the code.
Now when I run it and press the button it works as it should.
I will just need to run a timer for 1 second then activate the code in the code module.

Or, I guess I could trigger the code in the code module from Activity_Resume ?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Is the database creation an async task?

What do you mean by that ?

I am creating a whole heap of tables (approx. 10 of them) and loading a default record in each
B4X:
data = "CREATE TABLE table1 (id INTEGER PRIMARY KEY"
				For k = 1 To 200
	    			data = data & ", item" & k & " TEXT"
				Next
			data = data & ")"
			SQL1.execNonQuery(data) ' create the table	
			data = ""

			data = "INSERT INTO table1 VALUES (NULL"
				For k = 1 To 200
	    			data = data & ",''"
				Next
			data = data & ")"
			SQL1.execNonQuery(data) ' load the default data into this table
			data = ""
' Then run the same code above approx. 10 times, but with a different table name as it will serve a different purpose in the app.

Once the tables etc are created I wanted to then activate the sub in the Main activity.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I've used CallSubDelayed for this sort of thing in the past. It should call the sub once it's finished what it's currently doing.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…