Android Question Call from Class module to module outside the class

hakha4

Member
Licensed User
Longtime User
Hi. I have a class module creating buttons on a panel and a click event. From this I want to pass a property from the class to an outside module in Main. The sub in Main runs when button clicked BUT variable is empty. I tried different approaches but sent variable is always EMPTY. Can't figure out how to do from forum.

Class module
B4X:
Sub button_Click
 
    Log($"You clicked button ${mName}"$)
    Log(mBtn_no)
    Log(mTAG)' Shows correct value
   
    Main.Last_tag = mTAG' Last_tag Public declared in Main
    '######################### SEND RF CODE #######################################################
   
    CallSub2(Main, "sendRF",Main.Last_tag )' EMPTY
  '  CallSub2(Main, "sendRF",mTag )' EMPTY
  '  CallSubDelay2(Main, "sendRF",Main.Last_tag )' EMPTY
  '  CallSubDelay2(Main, "sendRF",mTag )' EMPTY
   
End Sub

Code in Main

B4X:
Sub sendRF(tmpval As String)

Log(tmpval)' EMPTY !
...............

End Sub

What is the way to call ? How is variables handled?
Any help appreciated
 

walterf25

Expert
Licensed User
Longtime User
Hi. I have a class module creating buttons on a panel and a click event. From this I want to pass a property from the class to an outside module in Main. The sub in Main runs when button clicked BUT variable is empty. I tried different approaches but sent variable is always EMPTY. Can't figure out how to do from forum.

Class module
B4X:
Sub button_Click
 
    Log($"You clicked button ${mName}"$)
    Log(mBtn_no)
    Log(mTAG)' Shows correct value
  
    Main.Last_tag = mTAG' Last_tag Public declared in Main
    '######################### SEND RF CODE #######################################################
  
    CallSub2(Main, "sendRF",Main.Last_tag )' EMPTY
  '  CallSub2(Main, "sendRF",mTag )' EMPTY
  '  CallSubDelay2(Main, "sendRF",Main.Last_tag )' EMPTY
  '  CallSubDelay2(Main, "sendRF",mTag )' EMPTY
  
End Sub

Code in Main

B4X:
Sub sendRF(tmpval As String)

Log(tmpval)' EMPTY !
...............

End Sub

What is the way to call ? How is variables handled?
Any help appreciated
Is your Main Activity, already running, or do you finish the Main Activity when you go to another Activity?
You can try declaring your global variables either in a separate Code Module or in the Starter Service, that way they will hold whatever value you assign last, and you can access them from any other Activity or Class.

Walter
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
CallSub2(Main, "sendRF",Main.Last_tag )' EMPTY
' CallSub2(Main, "sendRF",mTag )' EMPTY
You realize that if you use any of the CallSub methods and the Activity that your are targeting is not active, the CallSub does nothing? You may want to try modifying your main code to
B4X:
Sub sendRF(tmpval As String)
Log("I'm in Main's sendRF!")
Log(tmpval)' EMPTY !
...............

End Sub
just to make sure the sub is actually called.

The next two: I'm going to make one assumption: You are using CallSubDelayed2 instead of CallSubDelay2, since CallSubDelayed2 is an actually B4A method, the other is not.
' CallSubDelay2(Main, "sendRF",Main.Last_tag )' EMPTY
If Main.Last_tag is defined in Main's Globals instead of Process_Globals and CallSubDelayed2 has to start your Activity first, all variables in Globals will be wiped/set to their defaults.
' CallSubDelay2(Main, "sendRF",mTag )' EMPTY
That should have worked. Again, use the logging above and see if the sendRF sub is actually called. It could also be that you are doing more before doing the CallSubDelayed2 which wipes out mTag or you are using a method called CallSubDelay2 that does something weird.
 
Upvote 0

hakha4

Member
Licensed User
Longtime User
Is your Main Activity, already running, or do you finish the Main Activity when you go to another Activity?
You can try declaring your global variables either in a separate Code Module or in the Starter Service, that way they will hold whatever value you assign last, and you can access them from any other Activity or Class.

Walter
Hi, thanks for answer. Moved Public Last_tag to Starter and setting variable in class module, Last_tag = mTag. I can now read value ok in Main module. But for strange reason I can't set variable directly from Button_Click with either CallSubDelayed2 or CallSub2. Maybe not the most elegant solution but it works.
 
Upvote 0

hakha4

Member
Licensed User
Longtime User
You realize that if you use any of the CallSub methods and the Activity that your are targeting is not active, the CallSub does nothing? You may want to try modifying your main code to
B4X:
Sub sendRF(tmpval As String)
Log("I'm in Main's sendRF!")
Log(tmpval)' EMPTY !
...............

End Sub
just to make sure the sub is actually called.

The next two: I'm going to make one assumption: You are using CallSubDelayed2 instead of CallSubDelay2, since CallSubDelayed2 is an actually B4A method, the other is not.

If Main.Last_tag is defined in Main's Globals instead of Process_Globals and CallSubDelayed2 has to start your Activity first, all variables in Globals will be wiped/set to their defaults.

That should have worked. Again, use the logging above and see if the sendRF sub is actually called. It could also be that you are doing more before doing the CallSubDelayed2 which wipes out mTag or you are using a method called CallSubDelay2 that does something weird.
Hi and thank's for answer. I've used CallSubDelayed2 (not CallSubDelay2 ), type mistake but as said in reply to Walter I can't cast variable directly. I need to do some research how to use classes, new to this and do not fully understand how to implement.
Regards Håkan
 
Upvote 0
Top