B4J Question Callback in inline java code

wl

Well-Known Member
Licensed User
Longtime User
What is the way to have inline java code (#if java) call code in B4J in a console application ?

I tried

B4X:
import anywheresoftware.b4a.BA;
...
ba.raiseEventFromUI(this, "callback", "hello2");

but this did not call a B4J method "callback" in my current class ?

Thanks
 

Daestrum

Expert
Licensed User
Longtime User
If the inline code is in the same class as the one containing the sub you can just prefix the name with a '_'

ie
sub to be called
B4X:
Sub Test1(...)

can be called by (notice the name is ALWAYS lowercase)
B4X:
_test1(...);

But... this wont work if you obfuscate the code.
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Below a simplified version of the code, but my B4J code is not being called ("callback").

Is it possible to have the java code call a function in B4J (synchroneously) and receive the result of the B4J code ?

Thanks !

B4X:
public Sub StartServer
    Dim obj As JavaObject = Me
    obj.RunMethod("StartServer", Null)
End Sub

Public Sub callback (txt As String)
    Log (txt)
End Sub

#if java

public void StartServer() {
     ba.raiseEventFromUI(this, "_callback", null);
}

#end if
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Being a console application, will it support calling subs on a UIThread?

You also need to pass a parameter for the sub to be matched.

This works, although it throws a "Unexpected event (missing RaiseSynchronousEvents): " error running in debug

B4X:
#if java

public void StartServer() {
     ba.raiseEvent(this, "callback", "Text");
}

#end if
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Having tried it, it does support raiseEventFromUI, you just need to provide a parameter:
B4X:
public Sub StartServer
    Dim obj As JavaObject = Me
    obj.RunMethod("StartServer", Null)
End Sub

Public Sub callback (txt As String)
    Log (txt)
End Sub

#if java

public void StartServer() {
     ba.raiseEventFromUI(this, "callback", "Text");
}

#end if

Or
B4X:
public Sub StartServer
    Dim obj As JavaObject = Me
    obj.RunMethod("StartServer", Array("Param"))
End Sub

Public Sub callback (txt As String)
    Log (txt)
End Sub

#if java

public void StartServer(String text) {
     ba.raiseEventFromUI(this, "callback", text);
}

#end if
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
If the inline code is in the same class as the one containing the sub you can just prefix the name with a '_'

ie
sub to be called
B4X:
Sub Test1(...)

can be called by (notice the name is ALWAYS lowercase)
B4X:
_test1(...);

But... this wont work if you obfuscate the code.

I now understand what you mean ... I don't need to use import anywheresoftware.b4a.BA I can just reference the method just as it was local code.

Thanks
 
Upvote 0
Top