Answer the question
In order to leave comments, you need to log in
How in Java to call a method on the main thread when the background thread is finished?
I have a function to search a memory card for files of a certain type. It takes a long time and therefore is in the background thread. After its completion, I need to display the names of these files on the screen, and I can only do this from the main thread.
As a result, I have a task, after the completion of the background thread, call the main method and the join method will not work here, because I want the main thread to run while the background thread is running, rather than waiting for it to finish.
Answer the question
In order to leave comments, you need to log in
Use asynctask
Or, if you don't want to redo it, create on the main thread (just outside of your background thread, like a field in a class), Handler mainThread = new Handler(); and call already in your thread
mainThread.post(new Runnable(){
//your ui code
})
You can also subscribe to your event in the main thread.
And in the onPostExecute (AsyncTask) method, call this event and pass the result of the background process there
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question