Best way to create callback of calling a native jar function (SOLVED)

Hi, I have a java code class that have two functions, One do some work asynchronous and the other one is a call back listener for first function. from the project extension-android that @britzl provided, i found out that i can call a java function natively in a jar file so is there a way to have java call back that invoke lua function?
my point is i have a lua function that calls the first java fuction now i want to khow when that function work is complete, so i want to link it’s call back java function to another lua function, can i do this or should i have to check for java code work in a lua loop to see if it’s done.
if so, what is the best way to have lua loop that checks that, i khow i can write something like this:

local myFunction()

	 callFirstJavaFunction()
         local isCompleted = nil
 
	while (isCompleted == nil) do
		isCompleted = callJavaFunctionToCheckForEnding()
	end
 
	-- now java work is done
end

I’m new to defold and lua but i’m not sure about this ways performance, specially on a mobile device.

EDIT
I think i can create a wrapper in my java code that sort of take care of this in java env instead of doing it in lua, like call first function and awaits for result and then return the result of work, so this way in my lua code i just need to call that wrapper instead.
but for feature work i like to know the answer to my original problem.
Thanks in advance.

1 Like

You can call to and from Java code using JNI. This means that you can have some java code call a C function and the C function can in turn call some Lua code. Example:


2 Likes

Wow that’s exactly what i wanted thanks @britzl

1 Like

One important thing to note here is the communication between threads:
The Lua context is updated on the main thread.
Make sure you do your callbacks on the main thread too.

3 Likes

@Mathias_Westerdahl Thanks for the tip