AsyncTask - how to wait for onPostExecute () to complete before using updated values

I have an API in one bank that uses AsyncTask to do some work in the background. I need to wait until the result is returned, and then do another wok with this result.

I return to one class in the onPostExecute () method, and then the result is processed in the user interface thread, but the handler should be defined as a callback in another project as a whole, so I cannot just do the work in onPostExecute.

In the second project, I need to wait for AsyncTask to complete. And for the response to be processed, I need to display the results of the TextView handler in action in the second project.

Thread.sleep and using CountDownLatch do not work because processing is done in the user interface thread. Is there any way to do this?

+4
source share
1 answer

If I understand correctly, you have AsyncTask in one bank and user interface that need to be updated in another bank. If you run them as a single application, it does not matter where they are. Since you mentioned some callback, you can simply execute this callback in onPostExecute .

Below is an example sequence of events

  • Activity from jar 2 creates an asynchronous task and passes a callback that knows how to update the TextView as a parameter to the constructor
  • AsyncTask stores callback in instance variable
  • AsyncTask action in AsyncTask
  • AsyncTask in onPostExecute method with appropriate parameters
  • TextView Updates
+2
source

All Articles