V
V
vladocc2015-12-11 13:52:59
Java
vladocc, 2015-12-11 13:52:59

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

4 answer(s)
B
belozerow, 2015-12-11
@vladocc

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
})

N
Noobkesan, 2015-12-11
@Noobkesan

https://docs.oracle.com/javase/8/docs/api/java/uti...

E
Error_403_Forbidden, 2015-12-15
@Error_403_Forbidden

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

V
vladocc, 2016-12-22
@vladocc

The most logical and convenient way is to use AsyncTask, which has an onPosyExecute method that will be called when the background thread finishes and will be called from the base.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question