How do I use CallSub to go to antother screen

Hi there! I am new to B4A and is still running the trail version of B4A and I should actually buy full version.

This is the best program for creating apps that I have found compared with some other programs.

But I do not understand how to use CallSub function. My program I am trying to write is a quiz game and for every quiz I want the program to clear the screen so you can't see what the previous quiz was I tried to write:

Code for the Main file
B4X:
#Region  Project Attributes 
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim CheckBox1 As CheckBox
   Dim CheckBox2 As CheckBox

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Msgbox("Welcome to the quiz game!", "Quiz Game")
   Activity.LoadLayout("Layout1")
      
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)


End Sub


Sub Button1_Click
    
   If CheckBox1.Checked = True Then
   CheckBox1.Checked = False 
   Else
   CheckBox1.Checked = True
   Msgbox("The quiz has begun!", "Started Quiz")
   CallSub(Module1, "NextModule")
   End If
   
   
   If CheckBox2.Checked = True Then
   CheckBox2.Checked = False
   End If
   
End Sub
Sub Button2_Click

   If CheckBox2.Checked = True Then
   CheckBox2.Checked = False
   Else
   CheckBox2.Checked = True
   Msgbox("You where not ready to start quiz", "User Not Ready")
   End If
   
   If CheckBox1.Checked = True Then
   CheckBox1.Checked = False
   End If
   
   
   
End Sub

And the code for Module1 is:

B4X:
#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout1")
   
      Msgbox("Welcome", "Quiz Game")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Please can someone help how to do this. An example of this is Bike Race when you starts up Bike Race it first gives you two choices multiplayer and singleplayer and if you chose someone of them you gets a new cleared screen on your phone with challenges to do for the game.

:sign0085:
 
Last edited:
That sounds like a good idea. I saw the code when I scrolled down a bit and instead of including a new module you can have them in same module then it wouldn't be a problem to call the functions huh? Also if I want to call subroutine with the function CallSub then I can easily do it without that the program crashes when you runs it at your phone at it did for me.
 
Upvote 0
I checked out the code a bit more and about this line:

CallSubDelayed2(Activity2, "ShowList", "This is the title")

is ShowList within the quotes a function I have seen RefreshData, ShowList but nothing more. I just wondering for I need a function to get into next module if not RefreshData is working. :)
 
Upvote 0
CallSubDelayed function works but I get this error message about this code when I am running the app.

Message:

An error has occured in sub:
module1afterFirstLayout(B4A line: 23)
End Sub

java.lang.Expection: Sub test signature does not match
expected signature.

Module1
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim test As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout2")
   
      Msgbox("Welcome", "Quiz Game")
      End Sub
   
Sub test As Int
Msgbox("This is only a test", "TITLE")
'line 23 one step down
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

I'm don't understanding what's wrong is it anything I have forgotten to
call or some subroutine I need to do by myself?
 
Last edited:
Upvote 0

gregwa

Member
Licensed User
Longtime User
I believe that the error is talking about the fact that your definition says that the calling line should expect an Int in return...

B4X:
Sub test As Int
Msgbox("This is only a test", "TITLE")
'line 23 one step down
End Sub

If you remove the "As Int" from Sub test it should work correctly.

B4X:
Sub test
Msgbox("This is only a test", "TITLE")
'line 23 one step down
End Sub

I believe that If you do need to return a value you could do something like:

B4X:
returnedvalue = CallSubDelayed2(main,"Test",1)
 
Upvote 0
Gregwa :sign0085:: Do I need to do a Int or something of "returnedvalue" ? or is it a function built into B4A?. I tried to do this code first:

B4X:
Sub test
Msgbox("This is only a test", "TITLE")
End Sub

Also I skipped to decalare "As Int" but I still get's the same error message while I am running my app. Then I tried the other code with a return value.

GetBackToMainMenu was a function I created in "Main" in Process_Globals. Then I did a subroutine for GetBackToMainMenu like this if it was working.

B4X:
Sub GetBackToMainMenu
Msgbox ("working", "so far")
End Sub

And this is the code for the other with the return value. I do not know what to do because when I ran this code the B4A said:

Parsing code. 0.01
Compiling code. Error
Error compiling program.
Error description: Cannot assign void value.
Occurred on line: 26
returnedvalue = CallSubDelayed2(Main, "GetBackToMainMenu", 1)
Word: )


B4X:
Sub test 
Msgbox("This is only a test", "TITLE")
returnedvalue = CallSubDelayed2(Main, "GetBackToMainMenu", 1)
End Sub

Need a bit more help to get the app working because it is not so fancy if it comes up a error message while you are running your app.

:sign0013: that your code doesn't worked or if I have done anything wrong.
 
Upvote 0

gregwa

Member
Licensed User
Longtime User
Well, right now i'm totally confused.

GetBackToMainMenu was a function I created in "Main" in Process_Globals.

You can't put a subroutine inside of Process_Globals.

Why don't you post your code here (file | export as zip) so we can help understand what you are seeing.
 
Upvote 0
Oh, :sign0013: thought I had done that. But here it is:


Main
B4X:
#Region  Project Attributes 
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim GetBackToMainMenu As Int
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim CheckBox1 As CheckBox
   Dim CheckBox2 As CheckBox

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Msgbox("Welcome to Kevin's First application!", "Kevin's Application")
   Activity.LoadLayout("Layout1")
      
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)


End Sub

Sub GetBackToMainMenu
Msgbox ("working", "so far")
End Sub


Sub Button1_Click
    
   If CheckBox1.Checked = True Then
   CheckBox1.Checked = False 
   Else
   CheckBox1.Checked = True
   Msgbox("The quiz has begun!", "Started Quiz")
   CallSubDelayed2(Module1, "test", "TITLE")
   End If
   
   
   If CheckBox2.Checked = True Then
   CheckBox2.Checked = False
   End If
   
End Sub
Sub Button2_Click

   If CheckBox2.Checked = True Then
   CheckBox2.Checked = False
   Else
   CheckBox2.Checked = True
   Msgbox("You where not ready to start quiz", "User Not Ready")
   End If
   
   If CheckBox1.Checked = True Then
   CheckBox1.Checked = False
   End If
   
   
   
End Sub

Module1
B4X:
#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim test As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout2")
      Msgbox("Welcome", "Quiz Game")
      End Sub
   
Sub test 
Msgbox("This is only a test", "TITLE")
returnedvalue = CallSubDelayed2(Main, "GetBackToMainMenu", 1)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

gregwa

Member
Licensed User
Longtime User
Finally figured out the problems.

In Module1 you are using this line...
B4X:
returnedvalue = CallSubDelayed2(Main, "GetBackToMainMenu", 1)
There are two issues here. First, I told you wrong. You can not get a return value from any of the CallSubDelayed routines. However there is a way around that (see below). The biggest problem is that you are passing a parameter to the routine "GetBackToMainMenu" in the main module. However your sub is like this...
B4X:
Sub GetBackToMainMenu
   Msgbox ("working", "so far")
End Sub
There is no parameter. So you either remove the parameter from the CallSubDelayed routine and use CallSubDelayed instead of CallSubDelayed2 or add a parameter to the sub (GetBackToMainMenu) that you are calling.

The other error is in the sub test in module1. Again, you are calling it from the main module with a parameter but the actual sub has no parameter.

B4X:
Sub test(title As String) 
   Msgbox("This is only a test",title)
   CallSubDelayed(Main,"GetBackToMainMenu")
End Sub

The parameter (title) then is used to set the title of the message box from where ever you are calling it.

You also have defined "GetBackToMainMenu as Int" which is the name of the sub, so you don't want to do that. You also defined "Test" in module1 the same way.

Fix these issues and you won't hit any errors either at compile time or run time, with the code you now have.

Now, as to the workaround for a returned value.

When you call the routine in the main module, you can set a process_global variable which you can access by prepending "Main." to it. For example...

B4X:
Sub Process_Globals
    Dim GotBack As Int   
End Sub
Sub GetBackToMainMenu
   Msgbox ("working", "so far")
   GotBack = 1
End Sub
This sets the Process_Global variable to a 1. Now in your module1 code you can check it like this...
B4X:
Sub test(title As String) 
   Msgbox("This is only a test",title)
   CallSubDelayed(Main,"GetBackToMainMenu")
   Log("GotBack from Main = " & Main.GotBack)
End Sub
Which logs a '1'.

Hope this helps.
 
Upvote 0
:sign0013: but I'm still getting the same error here is the code. Do not understand what's wrong this time the app is working but no one wants to have a error message in it. Here is the code:

Main
B4X:
#Region  Project Attributes 
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim GotBack As Int
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim CheckBox1 As CheckBox
   Dim CheckBox2 As CheckBox

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Msgbox("Welcome to Kevin's First application!", "Kevin's Application")
   Activity.LoadLayout("Layout1")
      
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)


End Sub

Sub GetBackToMainMenu
Msgbox ("working", "so far")
GotBack = 1
End Sub


Sub Button1_Click
    
   If CheckBox1.Checked = True Then
   CheckBox1.Checked = False 
   Else
   CheckBox1.Checked = True
   Msgbox("The quiz has begun!", "Started Quiz")
   CallSubDelayed(Module1, "test")
   End If
   
   
   If CheckBox2.Checked = True Then
   CheckBox2.Checked = False
   End If
   
End Sub
Sub Button2_Click

   If CheckBox2.Checked = True Then
   CheckBox2.Checked = False
   Else
   CheckBox2.Checked = True
   Msgbox("You where not ready to start quiz", "User Not Ready")
   End If
   
   If CheckBox1.Checked = True Then
   CheckBox1.Checked = False
   End If
   
   
   
End Sub

Module1

B4X:
#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim test As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout2")
      Msgbox("Welcome", "Quiz Game")
      End Sub
   
Sub test (title As String)
Msgbox("This is only a test", title)
CallSubDelayed(Main, "GetBackToMainMenu")
Log("GotBack from Main = " & Main.GotBack)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

:sign0148:
 
Upvote 0

gregwa

Member
Licensed User
Longtime User
In your main activity code, change the code for Sub Button1_Click.


B4X:
Sub Button1_Click
    
    If CheckBox1.Checked = True Then
       CheckBox1.Checked = False 
    Else
       CheckBox1.Checked = True
       Msgbox("The quiz has begun!", "Started Quiz")
       CallSubDelayed2(module1, "test","TITLE OF MSGBOX")
    End If
    
    If CheckBox2.Checked = True Then
       CheckBox2.Checked = False
    End If
    
End Sub
 
Upvote 0
:sign0094: is wrong with this code it just says missing keyword. Missing keyword: End Sub and I have added that command in the end but it still says it. It's only on the line 40.

B4X:
#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim GetNextModule As Int
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim Choice1 As CheckBox
   Dim Choice2 As CheckBox
   Dim Choice3 As CheckBox
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("Layout2")
      Msgbox("First Question!", "Quiz Game")
      End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
      
Sub AnswerTheFirstQuestion1

   If Choice1.Checked = True 
   Msgbox("Correct!", "Quiz Game")
   CallSubDelayed2(Module2, "NextLevel2", "Quiz Game") 
   End If 'These line is the problem
End Sub

Sub AnswerTheFirstQuestion2
   If Choice2.Checked = True 
   Msgbox("You failed!", "Quiz Game")
   End If
End Sub
Sub AnswerTheFirstQuestion3

   If Choice3.Checked = True 
   Msgbox("You failed!", "Quiz Game")
   End If

End Sub
 
Upvote 0

gregwa

Member
Licensed User
Longtime User
in your last three subs that you just posted you forgot the "Then" at the end of the "If" statements.
 
Upvote 0
Top