I am trying to set a variable in an activity to the result of my AsyncTask. I have read that I cannot return a variable and that I would have to set up the variable in my activity and then change it in the onPostExecute
method.
My problem is that after the AsyncTask finishes I need to use that variable. I am having trouble with this because all the solutions I have tried so far cause the UI to freeze up and then go non-responding.
What I have tried so far:
while (!task.isCancelled()) {
// Wait...
}
And
while (variable == null) {
// Wait ...
}
Is what I am doing the best way to pass a variable from the AsyncTask to my Activity? If it is then how can I wait for the task to finish in the activity without locking up the UI?
If you are dependent on the result of an AsyncTask, you can do this.
Object result = asyncTask.execute().get();
The type of the result is the return type in your doInBackground() method. But then your main thread will be waiting until the task is complete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With