I'm actually trying to develop a function on Android to compute some things, however, this method could be (or not !) called from a Task.
This method has to Toast something at each step done, I found the method .runOnUiThread(Runnable ...)
. Written in my function directly.
This is working, but what if in the future someone doesn't want Toasts and prefers update a loading bar for example? It's why I would know if it's possible to pass to my function a method which will can run on the UI thread, and the easiest way as possible for the user of my function ? Maybe there is an efficient syntax with lambda expression?
Thanks in advance.
Thanks for your answer, I found a solution.
I have an interface to define a method action(int x, int y)
for example.
Then I have to pass in argument a type of this interface to my method, and this method just call the myInterface.action(..., ...)
with the updated values.
Then in the second Thread which is launching my method to compute things I have something like this :
myMethod(<some others args> ,
(int x, int y) ->
activity.runOnUiThread(() -> { /*Some code */ })
)
So I can choose directly where I call the method, what will be the behavior of this kind of handler. And in this case I use runOnUiThread()
because I'm in a Task.